Useful Commands: Introduction

In april ’06, I’ve decided to no longer use Nuxeo’s CPS and start a new online adventure with this blog.

Last week I’ve moved the majority of my stuff (articles and scripts mainly) from my objectis.org web site. There, I’ve compiled a list of linux commands I don’t want to forget, in the spirit of “Directory of Linux commands” from O’Reilly and pixelbeat.org Command Line Tips.

I’ve reordered all of the commands by theme and I’ll now publish each theme as a unique post, one theme per week, under the “Useful Commands: [Theme Name]” title. This week: Useful Commands: Files & Archives.

Archives commands

  • Extract .tar.gz file:
    tar xvzf ./file.tar.gz
    
  • Extract only one subdirectory and all its sub-content:
    tar -xvzf my-archive.tar.gz --wildcards "./directory/subdirectory*"
    
  • Create a .tar.gz file:
    tar cvzf file.tar.gz ./subfolder
    
  • Extract ./path/in/archive* subfolder content from all .tar.bz2 archives available in the current folder. Place the extracted content of each archive in a folder prefixed with the content- string:
    for ARCHIVE in `ls *.tar.bz2`; do DEST_FOLDER=content-`echo $ARCHIVE | cut -d '.' -f 1`; mkdir $DEST_FOLDER; tar -C $DEST_FOLDER -xvjf $ARCHIVE --wildcards "./path/in/archive*"; done
    
  • Extract all .gz files in the current folder:
    gunzip ./*.gz
    
  • Extract .tar.bz2 file:
    tar xvjf ./file.tar.bz2
    
  • Check a .bz2 file integrity:
    bzip2 --test ./file.bz2
    
  • Create a .zip archive of current directory, including all sub-dirs:
    zip -r archive.zip ./*
    
  • Create a 7-Zip archive (thanks to p7zip) of a folder, including all sub-directories:
    7za a archive.7z ./folder
    
  • Do the same as above, but split the archive in 50 Mib volumes:
    7za a -v50m archive.7z ./folder
    
  • Convert .tar.gz file to .tar.bz2 file:
    gzip -dc archive.tar.gz | bzip2 > archive.tar.bz2
    
  • Extract content from self-extracting shell archives:
    unshar archive.sh
    

File Management commands

  • Create several folder with a similar pattern:
    mkdir -p ./folder/subfolder{001,002,003}
    
  • Create a symbolic link (source):
    ln -s target link_name
    
  • List size in MiB of subfolders and files in the current folder and display them sorted by size:
    du -cm * | sort -nr
    
  • Case insensitive search from the current folder of all files that have the string dummy in their filename:
    find ./ -iname "*dummy*"
    
  • Recursive and case insensitive content search on non-binary files from the current folder:
    grep -RiI "string to search" ./*
    
  • Find all Jpeg images in the system but exclude /home and /var/lib directory:
    find / -path "/home" -prune -or -path "/var/lib" -prune -or -iname "*.jpg" -print
    
  • Get the list of the latest 10 modified files in the current folder tree:
    find ./ -printf "%TY-%Tm-%Td %TT %p\n" | sort | tail -n10
    
  • Same as above but sorted by latest access time:
    find ./ -printf "%AY-%Am-%Ad %AT %p\n" | sort | tail -n10
    
  • Rename all mp3 files in the current folder by adding a “sub-extension”:
    rename "s/\.mp3/\.my-sub-extension\.mp3/g" *.mp3
    
  • Convert all files in the current folder to lower case:
    rename 'y/A-Z/a-z/' *
    
  • Display the total size used by all PNG files in sub-directories:
    find ./ -iname "*.png" -exec du -k "{}" \; | awk '{c+=$1} END {printf "%s KB\n", c}'
    
  • List all files sharing the same name within the sub folders:
    find . -type f -printf "%f\n" | sort | uniq --repeated --all-repeated=separate
    

Dangerous Commands

  • Delete all .pyc and .pyo files in the system:
    find / -name "*.py[co]" | xargs rm
    
  • Delete all empty files and folders (I don’t know why, but I need to run this command several times to delete all empty folders):
    find ./* -empty -print -exec rm -rf "{}" \;
    
  • I used those commands when I import big quantity of files from a window user:
    find ./* -name "desktop.ini" -print -exec rm -f "{}" \;
    find ./* -name "Thumbs.db" -print -exec rm -f "{}" \;
    
  • Delete all files and folders in the current directory except the README.txt file:
    ls ./ -I "README.txt" | xargs rm -rf
    

Old Site to Blog: Merge in Progress

cps-to-wordpress I’m currently moving all the content from my old site to this blog. Here is the list of “new old” things I’ve manually imported:

Articles:

Linux Scripts:

Please have a look at the dedicated linux script page to get a short description of each of those scripts.