Shell

Display all f90 files in one color with 'ls':

You can add a similar thing for any extension. Below one is for f90 extensions and it displays f90 files in boldface.


LS_COLORS=$LS_COLORS:'*.f90=1:' ; export LS_COLORS

Renaming files in a directory when the command "rename" does not work:

for file in *; do var1=$(echo $file | sed 's/BEFORE/AFTER/g') ; mv $file $var1; done

Creating directories with sub-directories, sub-sub-directories ... at a time:

When you need to use this script: In general, for a project, we might need to do different kinds of calculations for the same type of systems. For example, for the same material, we might need to do electrical, magnetic, optical etc. properties and for a single project, you may need to use different materials. As all the materials will not be known at the starting of the project, we might need to add some materials. But, the properties calculated will be the same for all this new material also. In these cases, we want to keep the same directory order (tree path) either for each material (in some cases we may need for each property). For any of the above, you can follow the below process.

Solution with explanation:

1. Go to the top directory from which you need to create all the sub-directories. For example: "cd MATERIAL1". This is the directory that contains all the sub-directories which I want to create exactly at another place.

2. enter "cd .." and then type "dir -sR MATERIA1". The last command will give all the files and folders present in the folder MATERIAL1 recursively (R stands for it). As you can notice, all the folder names will have ":" at the end.

3. Now use this colon, ":", to grep only the folder names. For this, type "dir -sR MATERIAL1 | grep ':' > FOLDER_NAMES.sh", where we are redirecting the grepped folder names to a file "FOLDER_NAMES.sh"

4. Now open the file FOLDER_NAMES.sh and add "mkdir -p " at the beginning of each line (for this in escape mode give :%s/^/mkdir -p /g). Also change the MATERIAL1 name to the required material name (use :%s/MATERIAL1/MATERIAL2/g).

5. Finally, save the file and close it; type "chmod +x FOLDER_NAMES.sh" and run it in the terminal by "sh FOLDER_NAMES.sh", at the desired floder.

Summary of commands used:

cd MATERIAL1

cd ..

dir -sR MATERIAL1 | grep ':' > FOLDER_NAMES.sh

vi FOLDER_NAMES.sh

:%s/^/mkdir -p /g

:%s/MATERIAL1/MATERIAL2/g

:wq

chmod +x FOLDER_NAMES.sh

sh FOLDER_NAMES.sh (at the desired place)

Copying directories excluding some sub-directories to remote host:

rsync -avr -e "ssh -l user" --exclude 'folder_to_exclude' folder_to_copy remote:/path/to/directory