Usefull Commands: Files & Archives

  • Extract .tar.gz file:
    gtar xvzf ./file.tar.gz
    
  • Extract all .gz files in the current folder:
    gunzip ./*.gz
    
  • Extract .tar.bz2 file:
    tar xvjf ./file.tar.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
    
  • 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" ./*
    
  • Same as above but ignore .svn folders (don’t work with large folder content):
    grep -Ii "string to search" $(find . | grep -v .svn)
    

    Alternatively: use ack.

  • 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/' *
    

/!\ Below are listed all DANGEROUS COMMAND /!\

  • 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
    

1 Responses to “Usefull Commands: Files & Archives”


  • 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)

    Presumably because of directory structures similar to:

    /path/to/empty/folder1
    /path/to/empty/folder2
    

    folder1 and folder2 will be deleted on the first pass, leaving /path/to/empty. As find descends into each directory structure, /path/to/empty will be found to be non-empty on the first run, and empty on the second.

Leave a Reply

Additional comments powered by BackType