Sunday, March 18, 2012

Some interesting Unix commands - PART I


  1.  newname=$(ls *.log 2> /dev/null | sed 's/log/bad/' | sort -n )   --> this will list the files ending with .log and send errors to /dev/null. Then substitute the filename from log to bad and sort the files and store the filenames in the CRON_LOG variable. the actual file names will be untouched. 
    • sed options include: i-> edit files in place. Also, n-> silent output if not using edit file in place.
  2. Another way of doing the same as 1 is : perl -p -i -e 's///g'  *. You can also create a backup using  perl -p -i.bak -e 's///g'  *. This replaces within the existing file.
  3. echo ls *|tr [:lower] [:upper]  --> translates lower filenames to upper
  4. ls *.log|tr -d log ---> Deletes log from the filename
  5. ls file{2,3}*log ---> Only lists files: file2.log and file3.log
  6. ls *.log|cut -d "." -f1 --> prints out all the file names without the ".log"
  7. find /home/ -type f -name "*.log"
  8. newname=$(ls *.bad|cut -d '.' -f1,2,3) and then for z in $newname; do echo $z".log";done ---> the first command truncates .bad from filename abc.2323.gz.bad. The second one traverses through all the newnames and appends .log to them. So, the new filename is now abc.2323.gz.log

No comments:

Post a Comment