Moving multiple files having spaces in name (Linux)
No need to use a loop:
find . -maxdepth 1 -name "*$pattern*xlsx" -type f -exec mv {} $destination +
moving a file with spaces in name
Use escape characters for spaces. So change the variable fileName
to "this\ is\ my\ file". This ensures that the shell will ignore the spaces and won't consider it as a delimiter.
How to move multiple files with whitespace on linux
There's probably 400 ways to do this.. using find is likely the more efficient way, but:
You can use a for loop:
for i in `ls`; do mv $i dir/newdir/; done
Or a while loop with the file you created in your step 1:
ls -la|grep -e "May"|awk "{print $9, $10}" > some.files; cat some.files | while read mFILE; do mv $mFILE dir/newdir; done
Or with find (where +30 is greater than X days):
find ./ -mtime +30 -exec mv dir/newdir {} \;
OR, if you want to use awk and xargs:
ls -la|grep -e "May"|awk "{print $9, $10}" | xargs mv dir/newdir/
How to handle spaces in 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.
Moving files containing spaces with mv command
There is no way to have a regular parameter expansion that undergoes pathname expansion, but not word-splitting, so your attempt to put the pattern in entity_path
is going to fail. Either use the pattern directly,
mv -t "$path" "$entity_path"/*"$file_start"*"$file_end"*
or store the result of the pathname expansion in an array.
entities=( "$entity_path"/*"$file_start"*"$file_end"* )
mv -t "$path" "${entities[@]}"
How can I move multiple files to a directory while changing their names and extensions using bash?
No need for the loop, you can do this with just rename
and mv
:
rename -v 's/$/.pgp/' /opt/dir/ABC/allfile_123*
rename -v s/allfile_123/new_name/ /opt/dir/ABC/allfile_123*
mv /opt/dir/ABC/new_name* /usr/tst/output/
But I'm not sure the rename
you are using is the same as mine.
However,
since the replacement you want to perform is fairly simple,
it's easy to do in pure Bash:
for file in /opt/dir/ABC/allfile_123*; do
newname=new_name${file##*allfile_123}.gpg
mv "$file" /usr/tst/output/"$newname"
done
If you want to write it on a single line:
for file in /opt/dir/ABC/allfile_123*; do newname=new_name${file##*allfile_123}.gpg; mv "$file" /usr/tst/output/"$newname"; done
Linux Bash: Move multiple different files into same directory
You can do
mv car.txt bicycle.txt vehicle/
(Note that the /
above is unnecessary, I include it merely to ensure that vehicle
is a directory.)
You can test this as follows:
cd #Move to home directory
mkdir temp #Make a temporary directory
touch a b c d #Make test (empty) files ('touch' also updates the modification date of an existing file to the current time)
ls #Verify everything is there
mv a b c d temp/ #Move files into temp
ls #See? They are gone.
ls temp/ #Oh, there they are!
rm -rf temp/ #DESTROY (Be very, very careful with this command)
How can I copy a list of files with spaces in Linux terminal
There are several ways you can do that. You have to use double-quotes for expanding variables with spaces (backticks are used for executing inline).
The easiest is copying all the contents of ./iTunes/iTunes Music
to /home/me/Music
via cp:
cp -av "./iTunes/iTunes Music"/* /home/me/Music
If you want to build a list, manipulate it, and then copy only the contents of that list, then use while read
, as you did, but enclose each result in quotes:
find . -type f -name *.mp3 > output.txt
cat output.txt | while read file; do
cp "$file" ~/Music/WindowsMP3s/
done
Related Topics
How to Find or Calculate a Linux Process's Page Table Size and Other Kernel Accounting
View a Particular Line of a File Denoted by a Number
How to Communicate with a Linux Kernel Module from User Space Without Littering /Dev with New Nodes
Pipe Output to Use as the Search Specification for Grep on Linux
How to Create a File in Linux from Terminal Window
Ubuntu - Run Command on Start-Up with "Sudo"
.Htaccess Not Working (Mod_Rewrite)
How to Get the Physical Address from the Logical One in a Linux Kernel Module
Why Is Dd with the 'Direct' (O_Direct) Flag So Dramatically Faster
Why Crontab Uses or When Both Day of Month and Day of Week Specified
Difference Between Flat Memory Model and Protected Memory Model
What Happens to Other Processes When a Docker Container's Pid1 Exits
Undefined Reference to 'Clock_Gettime' Although '-Lrt' Is Given
Sighup for Reloading Configuration
How to Convert Spaces to Tabs in Vim or Linux
How to Simulate a Failed Disk During Testing
Gurus Say That Ld_Library_Path Is Bad - What's the Alternative