11 votes

Move files and folders recursively on Linux

When moving items from my thumb drive to my OSMC system, I've found the following very useful: find /media/Pi\ Hard\ 16GB/ -name '*' -exec mv -v {} /media/External\ HDD/Videos/ \; Explanation on how ...
Pi Hard's user avatar
  • 121
11 votes
Accepted

Move files from subdirectories into single directory and prefix original directory name

With Perl's standalone rename command: rename -n 's|/|_|; s|^|dest/|' */*.png Output: a/1.png renamed as dest/a_1.png a/2.png renamed as dest/a_2.png a/3.png renamed as dest/a_3.png b/1.png renamed ...
Cyrus's user avatar
  • 5,581
11 votes
Accepted

mv: target 'XXX' is not a directory

If you have any filenames with spaces (or tabs or newlines) in them, then you need to put $i in quotes.  If, for example, your 42nd file is named foo bar, then mv -v $i "Chapter$cptr.mp3" ...
Scott - Слава Україні's user avatar
10 votes
Accepted

bash move file relative to its position

With brace expansion echo mv /home/user/Downloads/{foo.bar,../Documents/} (remove echo if the result looks good). Note this generates /home/user/Downloads/../Documents/ as the target path. I ...
Kamil Maciorowski's user avatar
8 votes

Move all files from subdirectories to current directory?

mv */* . It will move all files from all subdirectories to current directory. If you need some cleanup, you could use find . -type d -empty -delete It will delete all empty subdirectories.
Nikandr Marhal's user avatar
7 votes
Accepted

Bash move and rename by parsing the output of find

Try: find . -mindepth 2 -type f -name '*.pdf' -exec bash -c 'f=${1#./}; mv "$1" "./${f//\//_}"' None {} \; This is safe for all file names, even ones with newlines in their names. How it works -...
John1024's user avatar
  • 16.9k
7 votes

Move files and folders recursively on Linux

It is possible to move instead of copy with rsync by using the --remove-source-files argument. This will preserve properties such as permissions and dates modified. It has the added benefit of ...
Tom Kelly's user avatar
  • 171
6 votes

Move files from subdirectories into single directory and prefix original directory name

With Bash 4 and recursive globbing (shopt -s globstar): for f in **/*.png; do dn=$(basename "$(dirname "$f")") bn=$(basename "$f") mv -- "$f" "../dest/${dn}_${bn}" done Note that dirname /foo/...
slhck's user avatar
  • 228k
5 votes

What happens if mv is interrupted?

If you want to interrupt mv because you want to disconnect from the terminal, you can just send it to background: * press Ctrl+Z # bg # disown
Курочка Ряба's user avatar
5 votes
Accepted

Files location after mv command without absolute paths

They'll be in the same directory, if you didn't provide any directory designation such as .. or ../.. So mv oldFile newFile stays in the same directory (basically renaming it), Note: If newFile ...
djsmiley2kStaysInside's user avatar
5 votes
Accepted

How to batch rename files by removing special character without using rename in linux shell

Most linux distro's include util-linux which gives you the rename command. You can do what you request by running these commands: rename ' ' _ *.mkv rename '(20' 20 *.mkv rename ').' . *.mkv You can ...
Sjon's user avatar
  • 216
4 votes

How to batch rename files by removing special character without using rename in linux shell

I use this one-liner to remove invalid characters in subtitle files: for f in *.srt; do nf=$(echo "$f" |sed -e 's/[^A-Za-z0-9.-]/./g;s/\.\.\././g;s/\.\././g'); test "$f" != "$nf" && mv "$f" "$...
Brian Kuepper's user avatar
4 votes

How to batch rename files by removing special character without using rename in linux shell

If you don't have the rename command, you can do this with a little shell scripting which busybox's ash also seems to support (this is recursive): find -type f -name \*.mkv | while read f; do n=${...
Sjon's user avatar
  • 216
4 votes

How to mv a folder in Linux retaining its mtime?

Another solution may be: rsync -a --remove-source-files source/data target/
Genjo's user avatar
  • 141
3 votes

Move files and folders recursively on Linux

cp -a --link ../public-back/* /public/. && rm -rf ../public-back So create hard links in the destination directory and remove the source dir. 'mv' simply will not work in your case, and in ...
Boudewijn's user avatar
3 votes

Find files with string

You should install pdfgrep package with: sudo apt-get install pdfgrep and run: pdfgrep -Hc 'RELAÇÃO DOS TRABALHADORES' target/* | cut -d : -f1 | xargs -I{} mv -i '{}' ./destination Test first! ...
techraf's user avatar
  • 4,902
3 votes

What is the fastest way to move a million images from one directory to another in Linux?

tar cf - dir1 | (cd dir2; tar xf -) tar cf - dir1 | ssh remote_host "( cd /path/to/dir2; tar xf - )" When you use 'cp' each file does a open-read-close-open-write-close. Tar uses different processes ...
maholt's user avatar
  • 41
3 votes

Move files and folders recursively on Linux

None of the answers in this thread fit my usecase, so I came up with one on my own as a shell script. At the heart of it is this function: recurse() { if [ ! -d "$1" ] || [ ! -e "$2&...
antichris's user avatar
  • 1,198
3 votes

Move a list of files(in a text file) to a directory?

I usually do it with cat filename | xargs -I {} mv {} /new/dir/path but not sure right now it works with spaces in filenames.
Flo's user avatar
  • 131
3 votes
Accepted

What is the behavior of mv on /?

I guess the target (../) was somewhere inside /home/ or another mountpoint where the filesystem other than / was mounted. Is this standard behavior for the mv command? Yes. If a file is ...
Kamil Maciorowski's user avatar
3 votes

Move files from subdirectories into single directory and prefix original directory name

As an alternative approach, you don't need external programs like rename, basename, etc - it can all be handled within bash parameter expansion:- find SourceDir ... | while read -r f; do mv "$f" "...
AFH's user avatar
  • 17.5k
2 votes

Move a list of files(in a text file) to a directory?

Try this: python -c "import shutil; [shutil.move(_.strip(), 'new') for _ in open('files.txt').readlines()];"
Miao Xu's user avatar
  • 21
2 votes
Accepted

Cannot move ./test to a subdirectory of itself, './test/test'

If the name is already in lower case, it will be given to the mv command twice, as you've seen. If the last argument to mv is a directory, it will be treated as the destination dir into which all the ...
Gabe's user avatar
  • 2,140
2 votes

How to batch rename files by removing special character without using rename in linux shell

I have been looking for a solution to this problem for a while now. I work on old closed systems that can't have new packages installed. I don't have the rename command. Finally I wrote a script that ...
ScriptKitty37's user avatar
2 votes
Accepted

mv command reports "X and Y are the same file", cifs filesystem

It turns out, the issue revolved around how the CIFS share was mounted. It appears that the inode was possibly being cached, thereby rendering most files duplicates as the script tried to write the ...
cgram's user avatar
  • 41
2 votes

mv command deleted source files even though the file was not fully moved

Were you moving across a mount point? Normally, the way mv works is to simply rename a file. A file's full name includes its absolute path, which is why renaming and moving are the same command. ...
anotherguy's user avatar
2 votes

Move files from subdirectories into single directory and prefix original directory name

As an alternative to rename, there is mmv, which uses standard shell patterns. From the man page: Mmv moves (or copies, appends, or links, as specified) each source file matching a from ...
Jochen Lutz's user avatar
2 votes
Accepted

What is the best and safest way to move files between HDDs in linux?

Don't remove the source till you're done. I typically use rsync -a source destination - I let it run once, run it a second time to confirm all the files are moved over and consistent. Safety is in ...
Journeyman Geek's user avatar
  • 130k
2 votes

moving files matching a pattern that catches the directory

With mv cannot move 'foo' to a subdirectory of itself is harmless in this case, you can ignore it. I mean mv will move the rest despite the warning. However the exit status will not be 0, this may be ...
Kamil Maciorowski's user avatar
2 votes
Accepted

Moving files with extension from source to destination

With your command mv *.o *.hi $srcpath $objpath you are moving every file with *.o or *.hi plus the folder src from the current $(pwd) folder to Obj If you want to move every *.o and *.hi which are ...
chloesoe's user avatar
  • 716

Only top scored, non community-wiki answers of a minimum length are eligible