klenwell information services : LinuxFiles

Ubuntu/Linux File Commands

return to DevLinux

Tar-Zip Directory

# zip /path/to/dir
$ cd /path/to
$ tar -zcvf dir.tar.gz dir

# unzip
$ tar -zxvf dir.tar.gz

# list files in zipped archive
$ tar -tvf dir.tar.gz | less

# read content of zipped file (without unzipping)
$ zcat file.gz | less


Find Largest Files / Directories

Files

Note path and size variables
Source: http://stackoverflow.com/questions/8943154/
$ find ./ -type f -size +5M -print0 | xargs -0 ls -1hsS
 11M ./foo/bar/some.log
6.4M ./sample.txt
5.1M ./another/snafu.zip

Directories

$ du -B M --max-depth=5 . | sort -n -r | less
898M    ./hg
536M    ./tmp
362M    ./hg/foo


Find Files

Basic Example

# basic find: find ROOT_DIR -name FILE_NAME
$ find . -name *.log


Find Image Files

$ find . -type f -name *.jpg -o -name *.png -o -name *.gif | less


Grep By File Type

ref: http://www.unix.com/302166849-post3.html
$ find . -type f -name '*.log' -exec grep -Hn 'foo' {} \; | less


Find/Chmod

# chmod only dirs
$ find /dir/to/chmod -type d -exec chmod 755 {} \;

# chmod only files
$ find /dir/to/chmod -type f -exec chmod 644 {} \;


Diff

Directories

# with preferred (hg) diff syntax
server:/opt/foo# diff -u conf/ /tmp/foo-conf-back/
diff: conf/cron: No such file or directory
diff: /tmp/foo-conf-back/cron: No such file or directory
Common subdirectories: conf/cron.d and /tmp/foo-conf-back/cron.d
diff -u conf/emailserver.properties /tmp/foo-conf-back/emailserver.properties
--- conf/emailserver.properties 2011-04-14 10:04:47.000000000 -0700
+++ /tmp/foo-conf-back/emailserver.properties      2011-05-03 09:53:42.000000000 -0700
@@ -1,3 +1 @@
-EmailServer=monty.python.com
-EmailTo=mpalin@mailhost.monty.python.com
+diff test
Only in /tmp/foo-conf-back/: lol.conf

# list only files that have changed
server:/opt/foo# diff -q conf/ /tmp/foo-conf-back/
diff: conf/cron: No such file or directory
diff: /tmp/foo-conf-back/cron: No such file or directory
Common subdirectories: conf/cron.d and /tmp/foo-conf-back/cron.d
Files conf/emailserver.properties and /tmp/foo-conf-back/emailserver.properties differ
Only in /tmp/foo-conf-back/: lol.conf


References

Using "find" (codecoffee.com)
Finding Large Files (cognitivesandbox.com)
Compress a Whole Linux Directory (cyberciti.biz)