This question already has answers here:
Find and xargs to correctly handle filenames with spaces in their names
(3 answers)
Closed 3 years ago.
I'm very new to shell script and I'm having issues with archiving files.
I have a folder with .xlsm files and I want those files that passed the retention period. I was able to archive except I'm having issues with those files having spaces with their filename eg. X y z.xlsm. below is my sample code.
find ${work_dir} -type f -mtime +${retention} | xargs tar -cvf ${Destination}/archive.tar
Any idea how to achieve it?
Thanks!
Use NUL as delimiter (-print0 for find, and --null for xargs).
find ${work_dir} -type f -mtime +${retention} -print0 | xargs --null tar -cvf ${Destination}/archive.tar
Related
This question already has answers here:
Linux find and delete files but redirect file names to be deleted
(5 answers)
Closed 1 year ago.
So im using the script
find /path/to/files/* -mtime +60 -exec rm {} \;
How can i collect the deleted files and transfer them into a logfile in Bash script
You could do something like:
find /path/... -print ... | tee -a <log.file>
The -print will print out all the hits, and the tee will append that to some log.file.
Side note: the * at the end of your /path/to/files/* seems superfluous.
Side note2: if you just want to delete the files, find has a built-in -delete.
This question already has answers here:
Moving multiple files having spaces in name (Linux)
(3 answers)
Exactly how do backslashes work within backticks?
(2 answers)
Closed 2 years ago.
I have the following script that renames files based on a specific file name string. The script was working fine until I had to apply it to a directory that contains a folder name with a space.
Script:
for file in `find /home/splunkLogs/Captin/PPM\ Images/PXT -type f -name '*.jpg'`; do mv -v "$file" "${file/-0.jpg/_Page_1.jpg}"; done
You'll notice the file name "PPM Images", which has a space. I added a backslash so the path would be readable, but I get the error "mv: cannot stat /home/splunkLogs/Captin/PPM: No such file or directory. I also tried putting the folder name in quotes in the path and received the same error. Can anyone guide me with a solution for handling filename spaces with the MV command?
So do not read lines using for. Read https://mywiki.wooledge.org/BashFAQ/001 .
find /home/splunkLogs/Captin/PPM\ Images/PXT -type f -name '*.jpg' |
while IFS= read -r file; do
mv -v "$file" "${file/-0.jpg/_Page_1.jpg}"
done
or better:
find /home/splunkLogs/Captin/PPM\ Images/PXT -type f -name '*.jpg' -print0 |
while IFS= read -r -d '' file; do
mv -v "$file" "${file/-0.jpg/_Page_1.jpg}"
done
Do not use backticks `. Using $(...) instead is greatly preferred.
This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 3 years ago.
I have several files with the extension *.php in different subfolders in the folder /root/Hello. I try to rename all .php files to .html but I want to keep the structure i.e. the path to the file should remain identical.
I found all files with the following command:
find /root/Hello -name "*.php"
But I don't know how I can rename all files with *.php to *.html and keep the structure I think I must use:
-exec
But I don't which argument I should use with -exec
use find:
find /path -depth -name "*.php" -exec sh -c 'mv "$1" "${1%.php}.html"' _ {} \;
This question already has answers here:
Moving multiple files having spaces in name (Linux)
(3 answers)
Closed 7 years ago.
I have a find command which returns two files. Is there anyway of then copying those files to another directory? My find command is below if that will help.
find "$TEST" -iname "DTWD_????.JPG" -printf "%f\n"
New to linux, thanks.
You just need to use the -exec argument
find "$TEST" -iname "DTWD_????.JPG" -printf "%f\n" -exec mv -t '/home' '{}' +
This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 7 years ago.
I've two files FileA and FileB. Can someone please let me know how to get time for latest created file in a folder in Unix?
Both for only two files and the general case of n files, you can use find:
find -type f -printf '%T# \n' | sort -n | tail -1
If the files need to match a pattern, you can use something like:
find -type f -name 'example*.txt' -printf '%T# \n' | sort -n | tail -1
This prints all modification times of files in the working directory, sorts them, then selects the last (largest) one.