Linux move to parent folder

Shell script to move all files from subfolders to parent folder

I’ve bunch of folders in folder A . I want to move files from all those folders to A . Or I want to cut all files from child folders and paste them in parent folder. How to do that?

I haven’t really tested this to be foolproof or anything, but have you tried simply entering this command from Folder A in a terminal: mv */* . ?

2 Answers 2

Go to your A directory and run

find . -mindepth 2 -type f -print -exec mv <> . \; 

which means «find all files in this directory and its sub-directories and execute mv with target directory . for each file found to move them to current directory.

virpara: now this your edition forgets all files in .dot directories. Revised it with -mindepth 2 instead to suppress warnings.

With GNU find you can be a little more elegant and not spawn a mv process for each file: find A -mindepth 2 -type f -exec mv -t A \ +

David, your version works until you hit maximum command line length limit, which isn’t that hard to do given how find prints out the paths, especially in case where you invoke it from directory other than A, like in your example.

This is very dangerous. if you have files with the same file name in different subfolders, you will overwrite all of these with the last find. Better to use mv with —backup=numbered: find . -mindepth 2 -type f -print -exec mv —backup=numbered <> . \;

Well you could create a file and name it «cutme» (to create a file called cutme in the terminal type nano cutme . To save it press CTRL+X then press ENTER.) for example and paste the following in it assuming that:

  1. You want to do this recursively (In subfolders and subfolders of those subfolders)
  2. You want to skip moving the script file
  3. You have permissions to move the files in that folder
  4. Files may or may not include spaces in their names

find * -type f -print -not -type d -and -not -regex ‘cutme’ -exec mv <> .. \;

Note the name cutme inside the line. It should be the same as the script you will run.

After creating the file and pasting the above line, run the following in the same folder as the script:

chmod +x cutme . This will give your new file the «Executable» flag so you can execute it like this: ./cutme .

Источник

How to move all files including hidden files into parent directory via *

Its must be a popular question but I could not find an answer. How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

this question has a duplicate at SU, with an even more correct answer (not the accepted one though): cp -r /path/to/source/. /destination

9 Answers 9

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

Читайте также:  Move files and subdirectories in linux

You can use these two commands together:

mv /path/subfolder/* /path/ # your current approach mv /path/subfolder/.* /path/ # this one for hidden files 
mv /path/subfolder/* /path/subfolder/.* /path/ 

(example: echo ab expands to a.b ab )

Note this will show a couple of warnings:

mv: cannot move ‘/path/subfolder/.’ to /path/.’: Device or resource busy mv: cannot remove /path/subfolder/..’: Is a directory 

Just ignore them: this happens because /path/subfolder/* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

cp -r /path/subfolder/. /path/ # ^ # note the dot! 

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

The braces are just a short cut for mv /path/subfolder/* /path/subfolder/.* /path/ , not strictly necessary to combine the two commands into one.

I get the following error: mv: overwrite `/path/.’? y mv: cannot move `/path/subfolder/.’ to `/path/.’: Device or resource busy mv: overwrite `/path/..’? y mv: cannot move `/path/subfolder/..’ to `/path/..’: Device or resource busy

@Dejan Just ignore it. . denotes current directory and .. denotes up directory. You must have noticed that all other files are moved.

«Just ignore the warning» may not always be a good idea. Right now I’m having a problem with a script in which I need to stop execution if any step fails — since this solution always causes an error, it kills my script. I need a way to determine if the mv command failed or not.

* works fine as a pattern to match hidden and unhidden files excluding . and .. but always returns itself as well even if there are some matching files. This seems to be a bug and can be worked around by setting nullglob ( shopt -s nullglob ). But if one does that one could set dotglob instead which seems favorable to me. Either should probably only be enabled temporarily.

I think this is the most elegant, as it also does not try to move .. :

mv /source/path/* /destination/path 

this would miss files like ..anything or . anything etc. — stackoverflow.com/a/31438355/2351568 contains the correct regex for this problem. || but anyway using shopt -s dotglob is still the better solution!

@DylanB don’t memorize it. remember that it matches whatever is in the curlybrackets, separated by commas. * would find all files starting with a or b such as «anatomy» and «bulldozer». The second match is just an empty match, equivalent to * , and the first match is equivalent to .[!.] , where the group [!.] means a group NOT starting with a . . This means .* but not ..* .

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

shopt -s dotglob # This would cause mv below to match hidden files mv /path/subfolder/* /path/ 

In order to turn off dotglob , you’d need to say:

Very helpful. Wanted to find out more but shopt is a builtin so man shopt doesn’t work and help shopt is very brief. But you can do bashman () < man bash | less -p "^ $1 "; >and then bashman shopt to read all about it straightforwardly. (Might have to hit n to jump down to the command if there are lines starting with shopt, as I found.)

Читайте также:  Быстрая очистка дисков linux

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n <> /path \; 

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/ 2) mv /path/subfolder/* /path/ 3) mv /source/path/* /destination/path 

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

sudo rsync -vuar --delete-after --dry-run path/subfolder/ path/ 

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

How do I move files and directories to the parent folder in Linux?

when it tries to move . (current directory) but that won’t cause any harm.

It will move all files from all subdirectories to the parent of the current directory, too. I’d use -maxdepth 1 to be sure.

You must have a directory called scripts in your parent directory AND in your current directory. You will have to rename this one before you move it.

It worked but you left one one very important bit of information — you must run this from the subdirectory. Also this will not delete the subdirectory itself so you must back up one directory and do a rmdir on the subdirectory.

I found this superior: find . -mindepth 1 -maxdepth 1 -exec mv -t .. — <> + (taken from unix.stackexchange.com/q/6393/93768). No error messages and actually working in my bash script.

I came here because I’m new to this subject as well. For some reason the above didn’t do the trick for me. What I did to move all files from a dir to its parent dir was:

It can’t be more simple than:

To also move hidden files:

mv is a command to move files, * means all files and folders and ../ is the path to the parent directory.

That moves ALL the files one level up.

The character * is a wildcard. So *.deb will move all the .deb files, and Zeitgeist.* will move Zeitgeist.avi and Zeitgeist.srt one folder up, since, of course, .. indicates the parent directory.

To move everything including folders, etc, just use * instead of *.*

zsh: argument list too long: mv ; bash: /usr/bin/mv: Argument list too long . Aparently it doesn’t work for 80k files

There is no need to change directories. Just include * at the end of path:

mv /my/folder/child/* /my/folder/ 

Above only moves non hidden files. To move only hidden files use .*

mv /my/folder/child/.* /my/folder/ 

Above two can be combined in to one command:

mv /my/folder/child/* /my/folder/ 

Assuming all your hidden files begin with dot followed by a letter or a number (which they should), you could use

Читайте также:  Самый сложный дистрибутив линукс

The .[A-Za-z0-9]* part is to make sure you don’t try to move . or .. along, which would fail.

In bash you can use shopt -s dotglob to make * match all files and move them simply by

This is not the best solution since the setting is permanent for the shell until you change it by

but I think it’s good to know.

Call it in a subshell: (shopt -s dotglob && mv * ..) . That way, the option is only local to that subshell.

Good answer — it’s simple, includes hidden files and doesn’t cause an error about copying ‘.’ and ‘..’

A method which causes no errors and works every time:

ls -1A . | while read -r file do mv "./$" .. done 
find . -maxdepth 2 -type f -exec mv <> .. \; 

I used a variation of above to move all the files from subfolders into the parent.

I’d got data in folders by year, but found by using metadata I could have them all in the same folder which made it easier to manage.

/data/2001/file_1 /data/2002/file_2 /data/2003/file_3 

It’s simple to move all files and folders to the parent directory in Linux.

Go to that folder and use this command:

For example, if your files and folders are as follows:

/home/abcuser/test/1.txt 2.txt 3.jpg 4.php 1folder 2folder 
cd /home/abcuser/test mv * /home/abcuser 

All your files and folders will move to the abcuser folder (parent directory).

Thanks @Gareth, was about to the same. Abhishek, please don’t post any unrelated links, where’s the sense in that? Also, check your formatting please. Additionally, /the full path does not work in Linux, you have to escape spaces with /the\ full\ path .

find -type f|while read line; do mv $line $; done 

Thanks for contributing an answer. While this might work in simple scenarios, piping find into while read is a bad way to use find , and better answers have already been posted.

There’s a lot of answers to this question, which proves the flexibility of Bash commands. However, to me a very simply solution that does the trick nicely is this one:

mv move; * select all files and folders; .[^.]* collects up the hidden files, with one dot at the start of their name; . * will select files that start with two dots followed by other characters; .. is the destination, which in this case is the parent folder.

Note, using .[^.]* and . * will ensure you only select those files with a single dot followed by something other than a dot, and those with two dots followed by other characters.

If you’d like to avoid trying to remember this, I suggest setting up an alias, such as, alias mvallup=»mv * .[^.]* . * ..» . Add this to ~/.bash_aliases . Now you can just type mvallup and it’s a done deal.

A shorter version of this was suggested by William Edwards, but it didn’t include hidden files. He gave an example for also moving hidden files, but that was not exampled as simply as it could have been ( mv /path/subfolder/* /path/ ) hence why I’m posting this very simple option.

Источник

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