Swapping file and directory names
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
2
down vote
favorite
I have a collection of files at ./date-and-time/fixed/path/filename
where date-and-time
and filename
are variable. I would like to move all these files to ./filename/date-and-time
. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.
command-line files batch-rename filename
add a comment |Â
up vote
2
down vote
favorite
I have a collection of files at ./date-and-time/fixed/path/filename
where date-and-time
and filename
are variable. I would like to move all these files to ./filename/date-and-time
. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.
command-line files batch-rename filename
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a collection of files at ./date-and-time/fixed/path/filename
where date-and-time
and filename
are variable. I would like to move all these files to ./filename/date-and-time
. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.
command-line files batch-rename filename
I have a collection of files at ./date-and-time/fixed/path/filename
where date-and-time
and filename
are variable. I would like to move all these files to ./filename/date-and-time
. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.
command-line files batch-rename filename
command-line files batch-rename filename
edited Feb 28 at 3:54
muru
130k19274467
130k19274467
asked Feb 28 at 3:23
Charles
222211
222211
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Something like this should work (note I have echo
ed the actual commands; please check carefully that it is doing the right thing before removing them)
#!/bin/bash
shopt -s nullglob
for file in */fixed/path/*; do
[[ -f "$file" ]] || continue
f="$file##*/"; d="$file%%/*"
echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the[[ ... ]]
in particular is bash-specific, although in this context[ ... ]
should work in either shell
â steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
add a comment |Â
up vote
2
down vote
I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename
with variable date-and-time
and filename
.
date-and-time
can be extracted via awk
:
$ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
date-and-time
filename
is a bit easier to get as it can be extracted by basename
:
$ basename ./date-and-time/fixed/path/filename
filename
From these two code segments, an untested script follows for moving the files in the way you describe when executed in .
. Please verify it before use.
#!/bin/bash
for i in ./*/fixed/path/*; do
date_and_time=$(echo "$i" |awk -F "/" 'print $2');
filename=$(basename "$i");
mkdir "$filename";
mv "$i" "$filename"/"$date_and_time";
done;
add a comment |Â
up vote
0
down vote
Like this :
mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time
Edit:
If you need to create more folders i recommend making a script something like this :
Before you start, I really recommend you make a backup of the folder you are trying to do this on.
cd into the folder you trying to operate on and then:
for file in *; do
if [[ -f "$file" ]]; then
mkdir "$file%.*"
mv "$file" "$file%.*"
fi
done
- Loop over all (*) the files in the current folder.
- create a folder (mkdir) from the file without its extension $file%.*
- move (mv) the file into that folder.
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Something like this should work (note I have echo
ed the actual commands; please check carefully that it is doing the right thing before removing them)
#!/bin/bash
shopt -s nullglob
for file in */fixed/path/*; do
[[ -f "$file" ]] || continue
f="$file##*/"; d="$file%%/*"
echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the[[ ... ]]
in particular is bash-specific, although in this context[ ... ]
should work in either shell
â steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
add a comment |Â
up vote
4
down vote
accepted
Something like this should work (note I have echo
ed the actual commands; please check carefully that it is doing the right thing before removing them)
#!/bin/bash
shopt -s nullglob
for file in */fixed/path/*; do
[[ -f "$file" ]] || continue
f="$file##*/"; d="$file%%/*"
echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the[[ ... ]]
in particular is bash-specific, although in this context[ ... ]
should work in either shell
â steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Something like this should work (note I have echo
ed the actual commands; please check carefully that it is doing the right thing before removing them)
#!/bin/bash
shopt -s nullglob
for file in */fixed/path/*; do
[[ -f "$file" ]] || continue
f="$file##*/"; d="$file%%/*"
echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done
Something like this should work (note I have echo
ed the actual commands; please check carefully that it is doing the right thing before removing them)
#!/bin/bash
shopt -s nullglob
for file in */fixed/path/*; do
[[ -f "$file" ]] || continue
f="$file##*/"; d="$file%%/*"
echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done
edited Feb 28 at 5:21
answered Feb 28 at 4:07
steeldriver
63.3k1198167
63.3k1198167
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the[[ ... ]]
in particular is bash-specific, although in this context[ ... ]
should work in either shell
â steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
add a comment |Â
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the[[ ... ]]
in particular is bash-specific, although in this context[ ... ]
should work in either shell
â steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
This worked perfectly once I switched from dash to bash. Thanks!
â Charles
Feb 28 at 4:42
@Charles oops sorry I should have mentioned that - the
[[ ... ]]
in particular is bash-specific, although in this context [ ... ]
should work in either shellâ steeldriver
Feb 28 at 4:51
@Charles oops sorry I should have mentioned that - the
[[ ... ]]
in particular is bash-specific, although in this context [ ... ]
should work in either shellâ steeldriver
Feb 28 at 4:51
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
No worries, my fault for not specifying. :)
â Charles
Feb 28 at 4:52
add a comment |Â
up vote
2
down vote
I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename
with variable date-and-time
and filename
.
date-and-time
can be extracted via awk
:
$ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
date-and-time
filename
is a bit easier to get as it can be extracted by basename
:
$ basename ./date-and-time/fixed/path/filename
filename
From these two code segments, an untested script follows for moving the files in the way you describe when executed in .
. Please verify it before use.
#!/bin/bash
for i in ./*/fixed/path/*; do
date_and_time=$(echo "$i" |awk -F "/" 'print $2');
filename=$(basename "$i");
mkdir "$filename";
mv "$i" "$filename"/"$date_and_time";
done;
add a comment |Â
up vote
2
down vote
I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename
with variable date-and-time
and filename
.
date-and-time
can be extracted via awk
:
$ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
date-and-time
filename
is a bit easier to get as it can be extracted by basename
:
$ basename ./date-and-time/fixed/path/filename
filename
From these two code segments, an untested script follows for moving the files in the way you describe when executed in .
. Please verify it before use.
#!/bin/bash
for i in ./*/fixed/path/*; do
date_and_time=$(echo "$i" |awk -F "/" 'print $2');
filename=$(basename "$i");
mkdir "$filename";
mv "$i" "$filename"/"$date_and_time";
done;
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename
with variable date-and-time
and filename
.
date-and-time
can be extracted via awk
:
$ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
date-and-time
filename
is a bit easier to get as it can be extracted by basename
:
$ basename ./date-and-time/fixed/path/filename
filename
From these two code segments, an untested script follows for moving the files in the way you describe when executed in .
. Please verify it before use.
#!/bin/bash
for i in ./*/fixed/path/*; do
date_and_time=$(echo "$i" |awk -F "/" 'print $2');
filename=$(basename "$i");
mkdir "$filename";
mv "$i" "$filename"/"$date_and_time";
done;
I'll assume the pattern for the path is always ./date-and-time/fixed/path/filename
with variable date-and-time
and filename
.
date-and-time
can be extracted via awk
:
$ echo './date-and-time/fixed/path/filename' |awk -F "/" 'print $2'
date-and-time
filename
is a bit easier to get as it can be extracted by basename
:
$ basename ./date-and-time/fixed/path/filename
filename
From these two code segments, an untested script follows for moving the files in the way you describe when executed in .
. Please verify it before use.
#!/bin/bash
for i in ./*/fixed/path/*; do
date_and_time=$(echo "$i" |awk -F "/" 'print $2');
filename=$(basename "$i");
mkdir "$filename";
mv "$i" "$filename"/"$date_and_time";
done;
answered Feb 28 at 4:04
dsstorefile1
1,312111
1,312111
add a comment |Â
add a comment |Â
up vote
0
down vote
Like this :
mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time
Edit:
If you need to create more folders i recommend making a script something like this :
Before you start, I really recommend you make a backup of the folder you are trying to do this on.
cd into the folder you trying to operate on and then:
for file in *; do
if [[ -f "$file" ]]; then
mkdir "$file%.*"
mv "$file" "$file%.*"
fi
done
- Loop over all (*) the files in the current folder.
- create a folder (mkdir) from the file without its extension $file%.*
- move (mv) the file into that folder.
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
add a comment |Â
up vote
0
down vote
Like this :
mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time
Edit:
If you need to create more folders i recommend making a script something like this :
Before you start, I really recommend you make a backup of the folder you are trying to do this on.
cd into the folder you trying to operate on and then:
for file in *; do
if [[ -f "$file" ]]; then
mkdir "$file%.*"
mv "$file" "$file%.*"
fi
done
- Loop over all (*) the files in the current folder.
- create a folder (mkdir) from the file without its extension $file%.*
- move (mv) the file into that folder.
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Like this :
mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time
Edit:
If you need to create more folders i recommend making a script something like this :
Before you start, I really recommend you make a backup of the folder you are trying to do this on.
cd into the folder you trying to operate on and then:
for file in *; do
if [[ -f "$file" ]]; then
mkdir "$file%.*"
mv "$file" "$file%.*"
fi
done
- Loop over all (*) the files in the current folder.
- create a folder (mkdir) from the file without its extension $file%.*
- move (mv) the file into that folder.
Like this :
mkdir ./filename/ && mv ./date-and-time/path/to/path-to-your-folder/* ./filename/date-and-time
Edit:
If you need to create more folders i recommend making a script something like this :
Before you start, I really recommend you make a backup of the folder you are trying to do this on.
cd into the folder you trying to operate on and then:
for file in *; do
if [[ -f "$file" ]]; then
mkdir "$file%.*"
mv "$file" "$file%.*"
fi
done
- Loop over all (*) the files in the current folder.
- create a folder (mkdir) from the file without its extension $file%.*
- move (mv) the file into that folder.
edited Feb 28 at 3:51
answered Feb 28 at 3:38
An0n
80418
80418
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
add a comment |Â
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
I was hoping for a solution that I wouldn't have to enter 1000 times with different paths and filenames.
â Charles
Feb 28 at 3:45
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
You got 1000 dif folders ? You can always use the * or do you need to create also 1000 new directory's ?
â An0n
Feb 28 at 3:48
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
Then i recommend using a script. Edited my answer.
â An0n
Feb 28 at 3:51
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2faskubuntu.com%2fquestions%2f1010489%2fswapping-file-and-directory-names%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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