working with files

Need to sort the first 10 files, by size

$ du -s * | sort -rn | head -10
OR
$ 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’
OR
$ ls | sed ‘/.\{21,\}/!d’

******
—– tar specific files (author unknown) —-

!/bin/sh

# 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.

flist=`ls`

# 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
fi
done

# 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 ———-

NOT WORKING:

$ 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.

WORKING:

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

******

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: