Disclaimer

This information HAS errors and is made available WITHOUT ANY WARRANTY OF ANY KIND and without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. It is not permissible to be read by anyone who has ever met a lawyer or attorney. Use is confined to Engineers with more than 370 course hours of engineering.
If you see an error contact:
+1(785) 841 3089
inform@xtronics.com

Bash


One liners

To find a sting in a directory of gzipped files - log files etc -

needle is the search term.

 gunzip -c  *.gz|grep -H needle

When you are running out of disk space - find the big files - first find huge directories:

du --human-readable | sort --human-numeric-sort -r | head

Then the file(s)

find /var/ -type f -iname "*" -printf '%s %p\n'| sort -nr | head -10

Rename files and directories - spaces to underscore

$ find -name "* *" -type d | rename 's/ /_/g'    # do the directories first
$ find -name "* *" -type f | rename 's/ /_/g'

Copy directory tree with only one file type

rsync -avm --include='*.mp3' -f 'hide,! */' from_dir/ /to_dir

The entire directory structure from from_dir/  is copied to /to_dir, but only the .mp3 files are copied.
-a ensures all permissions and times on files are unchanged.
-m will omit empty directories.
-v is for verbose output.
--include is a simplified filter rule. In this case hide keeps files from the transfer - the comma separates the prefix from a modifier

-f is a filter rule

-n prints out a dry run - it will tell you what it would do but not actually copy anything.

If you don't want the top directory use the trailing slash.

Save a deleted file

1) find the process+fd by looking directly in /proc:

ls -al /proc/*/fd/* 2>/dev/null | grep {filename}

2) Then a similar technique to @nickray's, with pv thrown in:

tail -c +0 -f /proc/{procnum}/fd/{fdnum} | pv -s {expectedsize} > {recovery_filename}

You may need to Ctrl-C when done (ls /proc/{procnum}/fd/{fdnum} will tell you that the file no longer exists)), but if you know the exact size in bytes, you can use pv -S to make it exit when the count is reached.




Top Page wiki Index

Email

(C) Copyright 1994-2019
All trademarks are the property of their respective owners.