Linux move directory except one

How to rename and move files and directories on Linux

Overview

In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.

Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.

As such, you will notice that commands used to perform actions on files are identical with directories.

Rename files on Linux

To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.

The mv command will take the source file specified and rename it to the target file.

To rename a file named student1 to student10, for example, you would run the following command.

Provided the file target is the same directory, all file attributes will remain, including permissions.

Moving a file on Linux

To move a file to another location we use the same method as renaming a file, except the file path should be different.

For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.

Moving and Renaming files on Linux

A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.

For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.

Moving Multiple files on Linux

The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.

For example, to move student1.txt and student2.txt to /var/students , you would run the following command.

Moving Directories on Linux

Moving directories work the same as moving files. We specify the source directory and give a target directory.

Читайте также:  Linux команда для проверки памяти

For example, to move a directory path /tmp/logs to

/data/logs you would run the following command.

Moving Multiple Directories on Linux

As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.

Verbose Output Flag

The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.

To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.

Do Not Overwrite Existing Files

To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.

In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.

Do Not Prompt to Confirm Overwrites

If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.

Источник

Linux command line. Move all files and directories in directory, except some files and directories

I have a folder A which has files and directories, I want to move all those files and directories to another folder B , except file , file2 , directory , and directory2 .

How can this be done?

7 Answers 7

(the (D) to include dot (hidden) files).

what i usually do

(assuming you have no ‘a’ file).

remove whatever file or directory to be kept in place.

You can use find with excluded expressions:

This solution is inspired by this question.

If ./A and ./B are on the same filesystem and if these files do not already exist in ./B :

. then this operation should just be atomic:

If they are not, then there are at least 8 additional cross-device copies done with the above set of commands.

$ sudo mv (source folder name) A B(destination foldername)

ex: create empty dir some sub folder a 1)sudo mkdir -p A/,c,d>

5)sudo mv a(folder) b(folder)

You could use a bash array to store which ones you want to skip and then use the containsElement function demonstrated here:

Источник

Linux command to move a directory

My old and new directory have same folders and files inside.

mv: cannot move `./xxxxxx’ to a subdirectory of itself

How can I move it?

5 Answers 5

You should use mv -if old/* new/ without the trailing * .

This is because it unrolled to

i.e. move everything into new/baz

This is not what you wanted.

It works. What are You trying to achieve? Could You please write a short example of what the input data should look like and what the output data should look like? The truth is I have no idea what You are trying to do 🙂 Help me help You.

Читайте также:  Sharedlibrary dll windows 10

note that mv a/* b/ don’t move files .* (file name start with ‘.’) in a/ to b/

If you are copying from an ext2/3/4 file system to a FAT32 file system, and a filename has an invalid character for FAT32 naming conventions, you get this terribly annoying and incorrect as hell error message. How do I know? I wrestled with this bug — yes, it’s a KERNEL BUG — for 6 hours before it dawned on me. I thought it was a shell interpreter error, I thought it was an «mv» error — I tried multiple different shells, everything. Try this experiment: on an ext file system, «touch ‘a:b'» them «mv» it to a FAT32 file system. Try it, you’ll enjoy (hate) the results. The same is true for ‘ ‘ (\074 and \076).

Thanks for «man mv» — that’s a real big help, don’t quit your day job.

Might be you got the answer but above answer is not working for me. and finally lots of researching I got the answer. (Issue is due to files-ownership)
and just put sudo before the command and its working. 🙂 Same thing for cp and mv command.

Not the answer you’re looking for? Browse other questions tagged linux command-line mv or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Move all files except one

How can I move all files except one? I am looking for something like:

where I move old stuff to new stuff -folder except Tux.png . !-sign represents a negation. Is there some tool for the job?

14 Answers 14

If you use bash and have the extglob shell option set (which is usually the case):

/Linux/Old/!(Tux.png Tux1.png Tux2.png)

/Linux/New/ It empties the whole Old -directory. What is wrong?

/path/to/target/folder should do

Put the following to your .bashrc

It extends regexes. You can then move all files except one by

Exceptions in relation to other commands

Note that, in copying directories, the forward-flash cannot be used in the name as noticed in the thread Why extglob except breaking except condition?:

so Backups.backupdb/ is wrong here before the negation and I would not use it neither in moving directories because of the risk of using wrongly then globs with other commands and possible other exceptions.

Читайте также:  Windows ноутбук отключить клавиатуру

I would go with the traditional find & xargs way:

-maxdepth 1 makes it not search recursively. If you only care about files, you can say -type f . -mindepth 1 makes it not include the

/Linux/Old path itself into the result. Works with any filenames, including with those that contain embedded newlines.

One comment notes that the mv -t option is a probably GNU extension. For systems that don’t have it

A quick way would be to modify the tux filename so that your move command will not match.

I think the easiest way to do is with backticks

Use backslash with ls instead to prevent using it with alias, i.e. mostly ls is aliased as ls —color.

Thanks @Arnold Roa

) since question had this

For bash, sth answer is correct. Here is the zsh (my shell of choice) syntax:

Requires EXTENDED_GLOB shell option to be set.

I find this to be a bit safer and easier to rely on for simple moves that exclude certain files or directories.

The following is not a 100% guaranteed method, and should not at all be attempted for scripting. But some times it is good enough for quick interactive shell usage. A file file glob like

(which will match all files with names starting with a, b or c) can be negated by inserting a «^» character first, i.e.

I sometimes use this for not matching the «lost+found» directory, like for instance:

Of course if there are other files starting with l I have to process those afterwards.

Источник

Linux Move Directory Command

I am new to Linux and command line. How do I move a directory under Linux operating systems?

To move SOURCE directory to a DEST directory use the mv command as follows:

In this example, move /home/vivek/data/ directory to /nas/home/vivek/archived/ directory:
$ mv /home/vivek/data/ /nas/home/vivek/archived/
OR
$ cd /home/vivek
$ mv data/ /nas/home/vivek/archived/
To get verbose output (explain what is being done), type:
$ mv -v /home/vivek/data/ /nas/home/vivek/archived/

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I Move Multiple Directories?

Use the following syntax to move dir1 and dir2 to /path/to/dest/ directory:
$ mv -v dir1 dir2 /path/to/dest/
$ ls -l /path/to/dest/
OR use the following syntax:
$ mv -v /home/vivek/dir1/ /home/vivek/dir2/ -t /nas/home/vivek/archived/

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Оцените статью