How to copy the results of a find command in linux [duplicate] - linux

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' '{}' +

Related

How to exclude some extension files when copy a folder [duplicate]

This question already has answers here:
Exclude files in a shell script that has a certain pattern
(2 answers)
Closed 3 months ago.
I need copy folderA to folder_B, but I want exclude all *.xml when copy.
How can I use a linux cmd to implement the requirement in ternimal?
I was asked to find a different way than cp -rf sourcedir/!(*.xml) dest_dir/
Suggesting to use find command:
cp $(find sourcedir -type f -not -name "*.xml") dest_dir
Suggesting to try and tune the find command:
find sourcedir -type f -not -name "*.xml"

Rename all files containing a specific string [duplicate]

This question already has answers here:
Bash command-line to rename wildcard
(2 answers)
Closed 7 months ago.
I want to rename files of the type file_name (1).extension to file_name.extension. How can i use rename to delete the " (1)" string ?
you can use the find [path_to_dir] -type f -name "*file_extension*" -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;
In my case, i find all .txt files in a folders, and rename it to filename_renamed.
find . -type f -name '*txt*' -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;

Linux FIND searching files with names within single quotes [duplicate]

This question already has answers here:
How can I store the "find" command results as an array in Bash
(8 answers)
Closed 1 year ago.
I am trying to save results of FIND command into array so then I can do some AWK commands with them.
My actual code is: files_arr=( "$(find "$1" -type f \( -name "\'*[[:space:]]*\'" -o -name "*" \) -perm -a=r -print )
this code should find all files with spaces and without spaces and return them to my array (and are readable also)
The PROBLEM is, when I have directory named: 'not easy' and inside this is directory are files: 'file one' and 'file two' so what I will get is: not easy/file one
what I want to get is: 'not easy'/'file one'I was thinking about using SED to add quotes but it would add quotes even if I had just simple one word file which doesnt have quotes in it.
Thank you for our advices.
Try this out :
mapfile -d '' files_arr < <(find . -type f -name "'*[[:space:]]*'" -perm -a=r -print0)
declare -p files_arr # To see what's in the array

Rename multiple files? [duplicate]

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"' _ {} \;

delete all files within a directory that are older than 1 day [duplicate]

This question already has answers here:
How to delete files older than X hours
(9 answers)
Closed 9 years ago.
I need to ensure that I have no old files left in my directory so what I think I do is
find . -type f -mtime +1 -delete
i got that from the find man page but then
find . -type f -mtime +1 -exec /bin/rm
but again, now told that find: -exec requires an argument - didn't iI pass this. So I started Googling and I found that my command needs to look likee this:
find . -type f -mtime +1 -exec /bin/rm -f {} +
and now I'm just wondering what the two {} s and the + sign are for. Can anyone help me here?
Thanks!
The {} stands for the name of the file(s) found.
The + sign (instead of a ;) means that this command accepts multiple file names in the same command, so that find can run much faster because it is run less times. The number of files added to each execution of the command is limited by the maximum length of the command line find is willing to use.

Resources