working with files

Need to sort the first 10 files, by size

$ du -s * | sort -rn | head -10
$ find . -type f -maxdepth 1 -print0 | xargs -0 du -s | sort -rn | head -10

Need to find files greater than 10MB

$ find . -type f -maxdepth 1 -size +10240k

Need to find the most recent 10 files (date of creation)

$ ls -clt | grep ^- | head -10

Need to find the name of all the files in their home directory that have more than 20 characters in their file names

$ printf “%s\n” * | awk ‘length($0) > 20’

… and if you want dot files as well:

$ printf “%s\n” * .* | awk ‘length($0) > 20’
$ ls | sed ‘/.\{21,\}/!d’

—– tar specific files (author unknown) —-


# Get the directory name to be tared up

echo -e “What is the directory you want to archive”
read archivedir

# Get the name of the archive to create (with tar extension)

echo -e “What is the name of the archive you want to create?”
read archname

# Get the maximum filesize you desire for the archive in bytes
echo -e “What is the maximum size in bytes for a file”
read maxsize

# move to the directory

cd $archivedir

# this is where we create the list of files to be archived.


# now test each one to see if it is less than the max for i in $flist; do
# test each file by awking out the size and comparing it
# to our maximum size. If it is smaller it goes into a list
# if it is larger we ignore it. If it is smaller we put it into
# a text file. I’m doing this because I intend to later make this
# able to use more than one directory interactively. Also
# if tar is mapped to tar -f and you can’t use the -A or -r
# switches with -f

filesize=`ls -l $i | awk -F\ ‘{ print $5 }’`
if (( $filesize > /tmp/tarlist.txt

# Now that we have a list of files to tar up. Do it.

tlist=`cat /tmp/tarlist.txt`
echo $tlist
tar -cf $archname $tlist

# Clean up!

rm -f /tmp/tarlist.txt


——– how to handle file names with spaces ———-


$ for arch in $(find /srv/www/ -iname ‘*’ -type f); do md5sum $arch; done
which works as long as there is no space in the file name.


$ find /srv/www -type f -exec md5sum {} \;
$ find /srv/www -type f -print0|xargs -0 md5sum




$screen ==> new session
$screen -S <session_name>
$screen -R ==> re-attach
$screen -X ==> attach to a non-detached
CTRL-a c ==> new window
CTRL-a k ==> kill window
CTRL-a CTRL-a ==> toggle between windows
CTRL-a S ==> split window
CTRL-a A ==> name window
CTRL-d ==> detach
CTRL-a ” ==> list windows

terminal shortcuts

Control-A = goto beginning of the line
Control-E = goto to the end of the line
ESC B = goto previous word
ESC F = goto next word
Control-H / backspace = delete previous character
Control-D = delete next character
ESC D = cut next word
ESC Backspace = cut previous word
Control-K = cut to end of line
Control-Y = paste last cut text
Control-L = redraw screen
Control-Q = resume output (XON)
Control-S = pause output (XOFF)
Control-C = stop current program
Control-Z = suspend current program
Up-arrow = show previously run command