Hi,
I have a requirement to move zero byte files to an archive folder. I have the below script and it works fine if I run it from where the file is present. But when I run the script from different folder, I am getting error that file is not present. Please help.
#!/bin/ksh
INBOUND_PATH=/home/prasanna
ARCHIVAL_PATH=/home/prasanna/archive
ls -l $INBOUND_PATH | awk -F " " '{if ($5 == 0) print $9}' | xargs -I '{}' mv '{}' $ARCHIVAL_PATH
The problem is I am printing $9 from awk which gives me the filename alone. If I could get file path prefixing file name, it should resolve my issue
RudiC
September 14, 2016, 4:45am
2
Why don't you prepend the source file in the mv
command with the $INBOUND_PATH
?
find $INBOUND_PATH -size 0 -print | xargs
or use -s : file is not zero size
using awk with $5 is not the safe way
RudiC,
I am now sure how to prepend source file. Please help on that.
Chakrapani,
I didnt went for find since the archival path is in a subfolder and it will picks up those files as well.
-maxdepth
is not supported in my version.
This should overcome the missing -maxdepth find option:
find "$INBOUND_PATH" -type d ! -name . -prune -o -type f -size 0 \
-exec sh -c 'for i do echo mv "$i" "'$ARCHIVAL_PATH'"; done' sh {} +
Remove "echo" to enable the move.
jlliagre:
This should overcome the missing -maxdepth find option:
find "$INBOUND_PATH" -type d ! -name . -prune -o -type f -size 0 \
-exec sh -c 'for i do echo mv "$i" "'$ARCHIVAL_PATH'"; done' sh {} +
Remove "echo" to enable the move.
There is a bug: -name .
only matches if the start directory is .
cd "$INBOUND_PATH" && find . -type d ! -name . -prune -o -type f -size 0 \
-exec sh -c 'for i do echo mv "$i" "'"$ARCHIVAL_PATH"'"; done' sh {} +
A bit shorter is
cd "$INBOUND_PATH" && find . ! -name . -prune -type f -size 0 \
-exec sh -c 'for i do echo mv "$i" "'"$ARCHIVAL_PATH"'"; done' sh {} +
I put another "quote" around the $ARCHIVE_PATH that avoids expansion+globbing in the current shell.
2 Likes
Thanks for the corrections MiG! I should have test the script I posted
1 Like
RudiC
September 22, 2016, 3:41am
9
What would be wrong with (untested!)
find "$INBOUND_PATH" ! -name . -prune -type f -size 0 \
-exec sh -c 'for i do echo mv "$i" "'"$ARCHIVAL_PATH"'"; done' sh {} +
find "$INBOUND_PATH" ! -name .
lists me all the files and current directory.
/home/pganesan/crypt
/home/pganesan/crypt/file1.txt
/home/pganesan/crypt/file2.txt
/home/pganesan/crypt/file_file.txt
/home/pganesan/crypt/test_file_new.txt
But find "$INBOUND_PATH" ! -name . -prune
gives me the current folder alone. Files are not displayed
/home/pganesan/crypt/
RudiC
September 22, 2016, 5:34am
11
You'll need to experiment a bit. Try
find "$INBOUND_PATH" \! -path "$INBOUND_PATH" -prune
1 Like
Thank you. It works now:
find "$INBOUND_PATH" \! -path "$INBOUND_PATH" -prune -type f -size 0 -exec sh -c 'for i do mv "$i" "'$ARCHIVAL_PATH'"; done' sh {} +