Some useful one-line Unix commands

These are some useful one-line Unix commands with a syntax that is easily forgotten. Commands in red are those that you can do a lot of damage with.

The paste command
  • Takes two files and concatenates each line. This is useful for forming command lines with more than one argument. For example, suppose you want to create a script to use 3dvolreg for a group of files. First create a list of files to be processed using the "ls" or "find" command. Then use the "paste" command: paste file1 file1 > tmp.bsh

    This will create a file with two columns, each with the same filenames. Now you can use the global replace command of a text editor to add the command and edit the out put filename.

ln and lndir
    These commands can be used to create links to files rather than copying the links themselves. You should use soft links rather than hard links. If hard links are created, the target file cannot be deleted until all of the hard links are deleted. This can make it very difficult to clear files off the disk. If soft links are used, the target file can be deleted as one would expect, and this will "break" the links. Moreover, if you alias "ls" to "ls -F" in your .bashrc file, all soft links will be flagged on the screen with an "@" symbol at the end (and directories will be flagged with a "/" at the end)>. Here is the usage: ln -s <target-file>
  • Create a soft link to the target file and give the link the same name as the file. ln -s <target-file>
  • Create a soft link to the target file and give the link a new name (link-name). ln -s <target-file> <link-name>
  • Create an entire directory of soft links to the target directory and its subdirectories. This is a good way to "copy" a set of data you want to use in a new analysis. The result will be a set of directories that new analyses can be written into without affecting the originals. ln -s <target-directory> <top-link-directory>
Examples of "find" commands:
  • Find all files that fit a pattern, say files with "_vr+orig" in them: find . -name "*_vr+orig*"
  • Remove all files that fit a pattern: find . -name "*_vr+orig*" -exec /bin/rm \{\} \;
  • Find files that end in *.bsh that contain the text "3dvolreg" find . -name "*.bsh" -exec egrep -H \{\} \;
  • Change permissions of all files in a directory and its subdirectories to give global read access: find . -name "*" -exec chmod 0777 \{\} \;
  • Find and remove all broken soft links in a directory tree: find . -name "<target-pattern>" -type l | (while read FN ; do if [ ! -e "$FN" ] ; then echo "$FN"; fi;

    <target-pattern> is a pattern with wild cards. Be sure to put it in double-quotes. For example, the pattern "*" will find all broken links, the pattern "run_*_m+orig*" will find every run with "_m" at the end, i.e. all runs motion-corrected by the preprocessing script.

Examples of awk (or gawk) commands:
  • Set a script variable to the directory that begins with "S" and ends in "_2DFAST" in the directory "anatomicals" DIR=`ls anatomicals` | gawk '{for(i=1;i<=NF;i++) if ($i ~ /S*_2DFAST]) print $i}'
Useful aliases and functions for your .bashrc file.
  • List the pulse sequence used in each p-file in a given directory and its subdirectories. function find_psdname() { find $* -name "P*.7" -exec print_header \{\} -print_name \; | grep psdname; }
  • Find P-files and print the patient ID function find_patid() { find $* -name "P*.7" -exec print_header \{\} -print_name \; | grep patid; }
  • Flag links and directories; color-code files by their type. alias ls="ls -F --color=auto"
  • Plot a 1D file containing motion covariates with reasonable options function plot_motion() { find . -name $* -exec 1dplot -sepscl -volreg -xlabel \{\} -yaxis -2:2:1:4 \{\}[1,2,3,4,5,6] \; ; }
  • Convert a DOS text file to a Unix format file. (i.e., strip off the carriage-returns. dos_to_unix {gawk 'BEGIN{FS="\r"}{print $1}' $* > $*.tmp; mv -f $*.tmp $*;}
Snippets of useful bash code to use in scripts.
  • Test if a file (named $file below) exists
        if [ ! -e $file ]
    then
        MISSING_FILES=1 # Skip this subject
    fi 
        
  • Create a log-file with todays date FILE=myrun_`date | gawk '{printf("%s%s%s",$6,$2,$3)}'`.log

    This command well create a log-file of the format myrun_2007Nov17.log

  • Get the brik-number corresponding to a give statistic. For example, to get the F-statistic corresponding to the "Go" condition in a specific file. get_brik_num <file-name> Go_Fstat For example to add two t-statistics, you could use the command 3dcalc -a ${subj1}+orig[get_briknum glm_out+orig Go#0_Tstat`] -b ${subj1}+orig[get_briknum glm_out+orig Go#0_Tstat`] -expr "a+b" -prefix output_file

    This version of the command will work with all subjects, regardless of how the model is defined or how the parameters are ordered.

Last modified February 24, 2011