I realize that any question asking how to delete files must be taken with great care.
My first answer was too hasty I didn't take the fact that the filelist could be malformed to be used with egrep. I edited the answer to reduce that risk.
That should work for the files that has no space in the name:
First rebuild your filelist to be sure to match the exact file name:
sed -e 's,^,^,' -e 's,$,$,' filelist > newfilelist
build the rm commands
cd your_directory
ls | egrep -vf newfilelist | xargs -n 1 echo rm > rmscript
Check if the rm script suits you (You can do it with "vim" or "less").
Then perform the action :
sh -x rmscript
If the files have spaces in their name (if the files have the "
in the name then this will not work) :
ls | egrep -vf newfilelist | sed 's,^\(.*\)$,rm "\1",' > rmscript
of course the filelist should not be in the same directory!
EDITED :
The Nathan's file list contained names that were matching all the files in the directory (like "html" matches "bob.html"). So nothing was deleted because egrep -vf
absorbed all the stream. I added a command to put a "^" and a "$" around each file name. I was lucky here that Nathan's file list was correct. Would have it been DOS formatted with CR-LF ended lines or with additional spaces, no files would have been preserved by the egrep and all been deleted.
You could use a simple sed expression:
sed -Ei 's/^([^_]+)_([^_]+)_/\1,\2,/' file
file
contents after modification:
ADB,AR,2006_07.pdf
ADBL,AR,2010_11.pdf
CBL,AR,2013_14.pdf
CZBIL,AR,2007_08.pdf
BOKL,AR,2015_16.pdf
A simplified approach would look as:
sed -i 's/_/,/; s/_/,/' file
Best Answer
With GNU
stat
:stat
displays information about the file-c
specifies the format,%s
gives the filesize in bytespaste -d+ -s
concats the output together line by line with a+
as delimiterbc
piped tobc
, it will be calculated together.Add a
-L
option tostat
, if for symlinks, you'd rather count the size of the file that the symlink eventually resolves to.That assumes a shell like
ksh
,bash
orzsh
with the$(<file)
operator to invoke split+glob on the content of a file.Here
list
is expected to be a space, tab or newline (assuming the default value of$IFS
) delimited list of file patterns (as in*.txt /bin/*
). For a list of file paths, one per line, you'd need to disable globbing and limit$IFS
to newline only, or with GNUxargs
: