How to move a folder into itself?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Ok, that question sounds silly. What I mean is this: I have a directory
foo/
bar.txt
baz.yzw
wun/
a.out
Now, I would like to basically add a directory in between, i.e. I would like to make it
foo/
var1/
bar.txt
baz.yzw
wun/
a.out
with the intent of also adding other stuff to foo
, but kept separate from the old contents.
I could of course do it like this:
$ mkdir foo-new
$ mv foo foo-new
$ mv foo-new foo
or
$ cd foo
$ mkdir var1
$ mv $(ls | grep -v var1) var1
but both seem inelegant and are error-prone.
Is there a better way to do it?
directory mkdir
add a comment |Â
up vote
2
down vote
favorite
Ok, that question sounds silly. What I mean is this: I have a directory
foo/
bar.txt
baz.yzw
wun/
a.out
Now, I would like to basically add a directory in between, i.e. I would like to make it
foo/
var1/
bar.txt
baz.yzw
wun/
a.out
with the intent of also adding other stuff to foo
, but kept separate from the old contents.
I could of course do it like this:
$ mkdir foo-new
$ mv foo foo-new
$ mv foo-new foo
or
$ cd foo
$ mkdir var1
$ mv $(ls | grep -v var1) var1
but both seem inelegant and are error-prone.
Is there a better way to do it?
directory mkdir
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Ok, that question sounds silly. What I mean is this: I have a directory
foo/
bar.txt
baz.yzw
wun/
a.out
Now, I would like to basically add a directory in between, i.e. I would like to make it
foo/
var1/
bar.txt
baz.yzw
wun/
a.out
with the intent of also adding other stuff to foo
, but kept separate from the old contents.
I could of course do it like this:
$ mkdir foo-new
$ mv foo foo-new
$ mv foo-new foo
or
$ cd foo
$ mkdir var1
$ mv $(ls | grep -v var1) var1
but both seem inelegant and are error-prone.
Is there a better way to do it?
directory mkdir
Ok, that question sounds silly. What I mean is this: I have a directory
foo/
bar.txt
baz.yzw
wun/
a.out
Now, I would like to basically add a directory in between, i.e. I would like to make it
foo/
var1/
bar.txt
baz.yzw
wun/
a.out
with the intent of also adding other stuff to foo
, but kept separate from the old contents.
I could of course do it like this:
$ mkdir foo-new
$ mv foo foo-new
$ mv foo-new foo
or
$ cd foo
$ mkdir var1
$ mv $(ls | grep -v var1) var1
but both seem inelegant and are error-prone.
Is there a better way to do it?
directory mkdir
asked May 17 at 16:13
leftaroundabout
31917
31917
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
5
down vote
accepted
$ cd foo
$ mkdir var1
$ mv * var1
The shell and mv
command are smart enough to not try to move the var1 directory into itself.
I should have just tried this... easy! It feels a bit inelegant because of the error messagemv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.
â leftaroundabout
May 25 at 16:02
add a comment |Â
up vote
4
down vote
You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo
$ mkdir var1
$ mv foo/* var1
$ mv var1 foo/
add a comment |Â
up vote
2
down vote
I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/
between foo
and its contents:
d=$(mktemp -d ./tmp.XXXXXX)
mkdir "$d"; mv foo "$d"/var1; mv "$d" foo
That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.
If we want to keep foo
intact, then we'll need to create var1
anew, and move the contents. Using Bash:
cd foo
mkdir var1
shopt -s dotglob # to get any dotfiles
mv * var1 # ignore the error about moving var1 to itself
Or, to avoid the error, use extglob
:
shopt -s extglob
mv !(var1) var1
Note that $(ls | grep -v)
will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).
add a comment |Â
up vote
1
down vote
You can use rsync
which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.
rsync -rv [--remove-source-files] foo/* foo/var1
There is an option -m, --prune-empty-dirs.
that prune empty directory chains from file-list
in my rsync man page, but I didn't get that work!
I put --remove-source-files
within braces to be [warn] about that.
to delete empty directories:
find foo/ -depth -type d -empty -execdir rmdir "" ;
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
$ cd foo
$ mkdir var1
$ mv * var1
The shell and mv
command are smart enough to not try to move the var1 directory into itself.
I should have just tried this... easy! It feels a bit inelegant because of the error messagemv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.
â leftaroundabout
May 25 at 16:02
add a comment |Â
up vote
5
down vote
accepted
$ cd foo
$ mkdir var1
$ mv * var1
The shell and mv
command are smart enough to not try to move the var1 directory into itself.
I should have just tried this... easy! It feels a bit inelegant because of the error messagemv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.
â leftaroundabout
May 25 at 16:02
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
$ cd foo
$ mkdir var1
$ mv * var1
The shell and mv
command are smart enough to not try to move the var1 directory into itself.
$ cd foo
$ mkdir var1
$ mv * var1
The shell and mv
command are smart enough to not try to move the var1 directory into itself.
answered May 17 at 16:30
John
11.2k11630
11.2k11630
I should have just tried this... easy! It feels a bit inelegant because of the error messagemv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.
â leftaroundabout
May 25 at 16:02
add a comment |Â
I should have just tried this... easy! It feels a bit inelegant because of the error messagemv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.
â leftaroundabout
May 25 at 16:02
I should have just tried this... easy! It feels a bit inelegant because of the error message
mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.â leftaroundabout
May 25 at 16:02
I should have just tried this... easy! It feels a bit inelegant because of the error message
mv: cannot move 'var1' to a subdirectory of itself, 'foo/var1'
, but on the command-line this is clearly the way to go.â leftaroundabout
May 25 at 16:02
add a comment |Â
up vote
4
down vote
You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo
$ mkdir var1
$ mv foo/* var1
$ mv var1 foo/
add a comment |Â
up vote
4
down vote
You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo
$ mkdir var1
$ mv foo/* var1
$ mv var1 foo/
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo
$ mkdir var1
$ mv foo/* var1
$ mv var1 foo/
You can simply make var1 directory, move all files/folders in foo to var1 and finally move var1 inside foo
$ mkdir var1
$ mv foo/* var1
$ mv var1 foo/
answered May 17 at 16:37
Arushix
9968
9968
add a comment |Â
add a comment |Â
up vote
2
down vote
I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/
between foo
and its contents:
d=$(mktemp -d ./tmp.XXXXXX)
mkdir "$d"; mv foo "$d"/var1; mv "$d" foo
That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.
If we want to keep foo
intact, then we'll need to create var1
anew, and move the contents. Using Bash:
cd foo
mkdir var1
shopt -s dotglob # to get any dotfiles
mv * var1 # ignore the error about moving var1 to itself
Or, to avoid the error, use extglob
:
shopt -s extglob
mv !(var1) var1
Note that $(ls | grep -v)
will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).
add a comment |Â
up vote
2
down vote
I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/
between foo
and its contents:
d=$(mktemp -d ./tmp.XXXXXX)
mkdir "$d"; mv foo "$d"/var1; mv "$d" foo
That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.
If we want to keep foo
intact, then we'll need to create var1
anew, and move the contents. Using Bash:
cd foo
mkdir var1
shopt -s dotglob # to get any dotfiles
mv * var1 # ignore the error about moving var1 to itself
Or, to avoid the error, use extglob
:
shopt -s extglob
mv !(var1) var1
Note that $(ls | grep -v)
will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/
between foo
and its contents:
d=$(mktemp -d ./tmp.XXXXXX)
mkdir "$d"; mv foo "$d"/var1; mv "$d" foo
That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.
If we want to keep foo
intact, then we'll need to create var1
anew, and move the contents. Using Bash:
cd foo
mkdir var1
shopt -s dotglob # to get any dotfiles
mv * var1 # ignore the error about moving var1 to itself
Or, to avoid the error, use extglob
:
shopt -s extglob
mv !(var1) var1
Note that $(ls | grep -v)
will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).
I would go with renaming the directory and moving it inside a new one, approximately as you did. To insert var1/
between foo
and its contents:
d=$(mktemp -d ./tmp.XXXXXX)
mkdir "$d"; mv foo "$d"/var1; mv "$d" foo
That works for the directory contents. However, it's a bit more complicated if we also consider the owner, access bits, ACLs, extended attributes, inode number etc. of the directory.
If we want to keep foo
intact, then we'll need to create var1
anew, and move the contents. Using Bash:
cd foo
mkdir var1
shopt -s dotglob # to get any dotfiles
mv * var1 # ignore the error about moving var1 to itself
Or, to avoid the error, use extglob
:
shopt -s extglob
mv !(var1) var1
Note that $(ls | grep -v)
will not include dotfiles, and it will not work if you have filenames with whitespace or newlines (for two different reasons).
answered May 17 at 16:31
ilkkachu
48.1k669133
48.1k669133
add a comment |Â
add a comment |Â
up vote
1
down vote
You can use rsync
which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.
rsync -rv [--remove-source-files] foo/* foo/var1
There is an option -m, --prune-empty-dirs.
that prune empty directory chains from file-list
in my rsync man page, but I didn't get that work!
I put --remove-source-files
within braces to be [warn] about that.
to delete empty directories:
find foo/ -depth -type d -empty -execdir rmdir "" ;
add a comment |Â
up vote
1
down vote
You can use rsync
which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.
rsync -rv [--remove-source-files] foo/* foo/var1
There is an option -m, --prune-empty-dirs.
that prune empty directory chains from file-list
in my rsync man page, but I didn't get that work!
I put --remove-source-files
within braces to be [warn] about that.
to delete empty directories:
find foo/ -depth -type d -empty -execdir rmdir "" ;
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use rsync
which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.
rsync -rv [--remove-source-files] foo/* foo/var1
There is an option -m, --prune-empty-dirs.
that prune empty directory chains from file-list
in my rsync man page, but I didn't get that work!
I put --remove-source-files
within braces to be [warn] about that.
to delete empty directories:
find foo/ -depth -type d -empty -execdir rmdir "" ;
You can use rsync
which will create the destination directory (only last level) if that doesn't already exist, but this will result empty directories which you will delete it later or keep.
rsync -rv [--remove-source-files] foo/* foo/var1
There is an option -m, --prune-empty-dirs.
that prune empty directory chains from file-list
in my rsync man page, but I didn't get that work!
I put --remove-source-files
within braces to be [warn] about that.
to delete empty directories:
find foo/ -depth -type d -empty -execdir rmdir "" ;
answered May 17 at 17:08
ñÃÂsýù÷
14.7k82361
14.7k82361
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444417%2fhow-to-move-a-folder-into-itself%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password