Hot answers tagged

169 votes

how to rename multiple files by replacing string in file name? this string contains a "#"

To replace # by somethingelse for filenames in the current directory (not recursive) you can use the (Perl-)rename utility: rename 's/#/somethingelse/' * Characters like - must be escaped with a \. ...
KrisWebDev's user avatar
  • 1,893
112 votes
Accepted

After accidentally renaming /usr, how do I rename it back?

Since you have set a password for root, use su and busybox, installed by default in Ubuntu. All of su's required libraries are in /lib. Busybox is a collection of utilities that's statically linked, ...
muru's user avatar
  • 71.2k
102 votes
Accepted

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that ...
MatthewRock's user avatar
  • 6,906
101 votes

Adding unzipped files to a zipped folder

Use the update flag: -u Example: zip -ur existing.zip myFolder This command will compress and add myFolder (and it's contents) to the existing.zip. Advanced Usage: The update flag actually ...
Ethan Strider's user avatar
90 votes
Accepted

I've just "mv"ed a 49GB directory to a bad file path, is it possible to restore the original state of the files?

When moving files between filesystems, mv doesn't delete a file before it's finished copying it, and it processes files sequentially (I initially said it copies then deletes each file in turn, but ...
Stephen Kitt's user avatar
82 votes

I lost files by using the "mv" command and I don't know where they are!

You created a directory called tp2 in your home directory, i.e. you created the directory ~/tp2. You then changed into ~/Downloads/221-tp2-public-main and started to move files with mv. Since you ...
Kusalananda's user avatar
  • 327k
74 votes
Accepted

renaming a huge folder: is it risky?

Changing the name on a folder is safe, if it stays within the same file system. If it is a mount point (/data kinda looks like it could be a mount point to me, check this with mount), then you need ...
Kusalananda's user avatar
  • 327k
73 votes
Accepted

Why is mv so much faster than cp? How do I recover from an incorrect mv command?

If a directory is moved within the same filesystem (the same partition), then all that is needed is to rename the file path of the directory. No data apart from the directory entry for the directory ...
Kusalananda's user avatar
  • 327k
71 votes

Why do I get permission denied when using mv although directory rights are correct?

I was using Windows Subsystem for Linux. I had the directory open in a different bash instance. Closing it let me move the directory.
chris's user avatar
  • 809
67 votes
Accepted

How can I copy a directory and rename it in the same command?

You should be able to do just cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21 However, if the target directory already exists, this would append the final part of the source path ...
ilkkachu's user avatar
  • 135k
55 votes

how to rename multiple files by replacing string in file name? this string contains a "#"

find the list of files and then replace keyword. below is example find . -name '*jpg' -exec bash -c ' mv $0 ${0/\#U00a9NBC/safeNBC}' {} \;
Sujit Dhamale's user avatar
50 votes
Accepted

How can I change a file to a directory? Files lost after find+mv

Adding a slash at the end of the destination path /opt/alfresco/archived/2020-01-07 would have made the mv command error out, as the 2020-01-07 directory evidently does not exist. This would have ...
Kusalananda's user avatar
  • 327k
49 votes
Accepted

What is the point of mv -f when default behavior already overwrites files?

The usage of -f is more clearly described in the man page from 4BSD, which was where the -f and -i options were added: If file2 already exists, it is removed before file1 is moved. If file2 has a ...
Mark Plotnick's user avatar
48 votes
Accepted

Best practice to continue mv

Forget about trying to reinvent rsync, and use rsync. sudo rsync -av /location/to/drive1/ /location/to/drive2/ Make sure you use a trailing slash on the source, otherwise it would copy to /location/...
Gilles 'SO- stop being evil''s user avatar
47 votes

Solving "mv: Argument list too long"?

If working with Linux kernel is enough you can simply do ulimit -S -s unlimited That will work because Linux kernel included a patch around 10 years ago that changed argument limit to be based on ...
Mikko Rantalainen's user avatar
46 votes

Using mv command to move a file from one folder to another one - Strange example

TL;DR This forces the destination to be a directory that already exists or the command to fail without effect and is used as a fail-safe. The meaning of . Every directory in typical Unix/Unix-like ...
A.B's user avatar
  • 33.4k
39 votes

how to rename multiple files by replacing string in file name? this string contains a "#"

To escape # from the shell, just use single quotes ('#'), double quotes ("#"), or backslash (\#). The simplest in your case would be to use the rename command (if it is available): rename '#U00a9' ...
mik's user avatar
  • 1,332
38 votes

How to integrate mv command after find command?

For the benefit of OS X users coming across this question, the syntax in OS X is slightly different. Assuming you do not want to search recursively in subdirectories of path_A: find path_A -maxdepth ...
mannykary's user avatar
  • 481
36 votes
Accepted

Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up

Your file will be fine. Renaming a file will not alter the file's contents in any way whatsoever. In fact, you would still be able to successfully extract the contents of your compressed tar archive ...
Kusalananda's user avatar
  • 327k
35 votes

Moved /bin contents to /usr/bin, possible to undo?

On Linux (and on most other systems, though POSIX doesn't give you that guarantee unless the move was across file systems), that would have updated their ctime, so assuming none of the other ones in /...
Stéphane Chazelas's user avatar
35 votes
Accepted

How to mv .bin bin

If you still have a root shell open, run cd / /.bin/mv .bin bin Your shell can’t find mv because it’s no longer on the path; giving the full path to it will allow it to run. (As a general rule, it’...
Stephen Kitt's user avatar
35 votes

After accidentally renaming /usr, how do I rename it back?

In addition to muru's answer: you could have used some rescue boot USB key to repair your system; e.g. if your system is some Debian or Ubuntu, boot the installation USB key in rescue mode, and do ...
Basile Starynkevitch's user avatar
31 votes
Accepted

A filename has an asterisk for some reason - it won't be changed and content not executed

The asterisk is not actually part of the filename. You are seeing it because the file is executable and your alias for ll includes the -F flag: -F Display a slash ('/') immediately ...
jesse_b's user avatar
  • 36.4k
30 votes

How can I move all `marked` files to another directory in ranger?

With the files / directories marked, press dd, then navigate to the directory you want to paste them in and press p.
Chris Stryczynski's user avatar
30 votes
Accepted

mv: rename <oldname> to <newname>/<oldname>: Invalid argument

The filesystem on your NAS is case-insensitive¹, so OldHDD and oldhdd are the same file. But your operating system is case-sensitive, so it considers those different names. The mv command asks the ...
Gilles 'SO- stop being evil''s user avatar
28 votes

Why do I get permission denied when using mv although directory rights are correct?

Do note that, when in folder a, moving b to c, the folder permissions of a determine what you can do. In this case, the permissions on . will be most important. Observe that the permissions are more ...
Konerak's user avatar
  • 1,451
28 votes
Accepted

Change a directory name in a Github repository remotely, directly from local Linux Git?

The fatal error message indicates you’re working from somewhere that’s not a clone of your git repository. So let’s start by cloning the git repository first: git clone https://github.com/benqzq/...
Stephen Kitt's user avatar
28 votes

I lost files by using the "mv" command and I don't know where they are!

The GNU implementation of the mv command (the one found on Ubuntu) has an explicit mv -t myDir option that checks that myDir is specifically an existing directory. This avoids the ambiguity between mv ...
Paul_Pedant's user avatar
  • 8,431
25 votes
Accepted

bash: moving files with spaces

Never, ever use for foo in $(cat bar). This is a classic mistake, commonly known as bash pitfall number 1. You should instead use: while IFS= read -r file; do mv -- "$file" "new_place/$file"; done &...
terdon's user avatar
  • 238k
24 votes
Accepted

What's the difference between 'rename' and 'mv'?

It's basically what it says on the lid, for both. mv is a standard utility to move one or more files to a given target. It can be used to rename a file, if there's only one file to move. If there are ...
ilkkachu's user avatar
  • 135k

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