Skip to main content

Posts

Showing posts with the label for loop

Inline Shell Script with a For Loop

Often I'm asked how to traverse a list of items in a file. You can easily go through a list of items using a for loop. Here's an example of copying selected contents of originalDir to destinationDir via the command line. #sh #for i in `cat   /home/esofthub/mylist.dat ` #do #cp -pr / originalDir /$i / destinationDir /. #echo $i done #done sh – shell cat – lists each item in the list one iteration at a time cp – the copy utility for a local workstation (plain files and directories, no symbolic links) -pr – these options preserve the permission, "p" and copies recursively, "r" echo – lists the item copied

Strip a Prepended String From a Filename - Unix

I had a process that was pre-pending by design its PID to a file name string. But unfortunately, it caused a few unexpected problems when this handling process inadvertently terminated. I had to rename the files without the defunct pre-pended PID string and then reprocess them (several thousand). Here is what I used as a quick and dirty procedure. # csh # ls prependPID_filename_etc1 prependPID_filename_etc2 prependPID_filename_etc3 prependPID_filename_etc4 # foreach filename (prependPID*) ? mv $filename `echo $filename | sed 's/prependPID_//'` ? end # ls filename_etc1 filename_etc2 filename_etc3 filename_etc4 # zsh # ls prependPID_filename_etc1 prependPID_filename_etc2 prependPID_filename_etc3 prependPID_filename_etc4 # for i in prependPID* for> mv $i `echo $i | sed 's/prependPID_//'` # ls filename_etc1 filename_etc2 filename_etc3 filename_etc4