Renaming the files with prepending parent directory name [closed]

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Need help in mass renaming files that prepending their parent directory name to them without using rename command.
e.g.
/tmp/2017-09-22/cyber.gz
/tmp/2017-09-23/cyber.gz
/tmp/2017-09-24/cyber.tar
Also renamed files has to be copy in /tmp/archive without impacting above original files.
Looks like below
/tmp/archive/2017-09-22_cyber.gz
/tmp/archive/2017-09-23_cyber.gz
/tmp/archive/2017-09-24_cyber.tar
bash rename
closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
0
down vote
favorite
Need help in mass renaming files that prepending their parent directory name to them without using rename command.
e.g.
/tmp/2017-09-22/cyber.gz
/tmp/2017-09-23/cyber.gz
/tmp/2017-09-24/cyber.tar
Also renamed files has to be copy in /tmp/archive without impacting above original files.
Looks like below
/tmp/archive/2017-09-22_cyber.gz
/tmp/archive/2017-09-23_cyber.gz
/tmp/archive/2017-09-24_cyber.tar
bash rename
closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Need help in mass renaming files that prepending their parent directory name to them without using rename command.
e.g.
/tmp/2017-09-22/cyber.gz
/tmp/2017-09-23/cyber.gz
/tmp/2017-09-24/cyber.tar
Also renamed files has to be copy in /tmp/archive without impacting above original files.
Looks like below
/tmp/archive/2017-09-22_cyber.gz
/tmp/archive/2017-09-23_cyber.gz
/tmp/archive/2017-09-24_cyber.tar
bash rename
Need help in mass renaming files that prepending their parent directory name to them without using rename command.
e.g.
/tmp/2017-09-22/cyber.gz
/tmp/2017-09-23/cyber.gz
/tmp/2017-09-24/cyber.tar
Also renamed files has to be copy in /tmp/archive without impacting above original files.
Looks like below
/tmp/archive/2017-09-22_cyber.gz
/tmp/archive/2017-09-23_cyber.gz
/tmp/archive/2017-09-24_cyber.tar
bash rename
bash rename
edited Oct 9 '17 at 5:26
ñÃÂsýù÷
15.6k92563
15.6k92563
asked Oct 4 '17 at 0:22
Nirmal
42
42
closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.
find * -path "archive" -prune -o -type f -exec
bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;
-path "archive" -pruneis excludingarchivedirectory itself.$1///_is replacing Slash/with Underscore_. The1is point to thefind_bashfirst parameter which is file path passing by.
(Replace echo with cp to copy or mv to move the files)
Directory structure:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
After executing the command:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
âÂÂâÂÂâ 2017-09-22_cyber.gz
âÂÂâÂÂâ 2017-09-23_cyber.gz
âÂÂâÂÂâ 2017-09-24_cyber.tar
add a comment |Â
up vote
1
down vote
Using a short shell script:
#!/bin/sh
archive='/tmp/archive'
mkdir -p "$archive"
for name in /tmp/????-??-??/*; do
dirdate=$name%/* # dirdate=$(dirname "$name")
dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")
newname="$archive/$dirdate_$name##*/"
if [ ! -e "$newname" ]; then
echo cp "$name" "$newname"
fi
done
This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.
If the new name does not exist inside the archive directory, the file is copied there.
Remove echo from the above script to actually perform the copying.
For the given example, the script will perform the following operations:
cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.
find * -path "archive" -prune -o -type f -exec
bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;
-path "archive" -pruneis excludingarchivedirectory itself.$1///_is replacing Slash/with Underscore_. The1is point to thefind_bashfirst parameter which is file path passing by.
(Replace echo with cp to copy or mv to move the files)
Directory structure:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
After executing the command:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
âÂÂâÂÂâ 2017-09-22_cyber.gz
âÂÂâÂÂâ 2017-09-23_cyber.gz
âÂÂâÂÂâ 2017-09-24_cyber.tar
add a comment |Â
up vote
1
down vote
If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.
find * -path "archive" -prune -o -type f -exec
bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;
-path "archive" -pruneis excludingarchivedirectory itself.$1///_is replacing Slash/with Underscore_. The1is point to thefind_bashfirst parameter which is file path passing by.
(Replace echo with cp to copy or mv to move the files)
Directory structure:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
After executing the command:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
âÂÂâÂÂâ 2017-09-22_cyber.gz
âÂÂâÂÂâ 2017-09-23_cyber.gz
âÂÂâÂÂâ 2017-09-24_cyber.tar
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.
find * -path "archive" -prune -o -type f -exec
bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;
-path "archive" -pruneis excludingarchivedirectory itself.$1///_is replacing Slash/with Underscore_. The1is point to thefind_bashfirst parameter which is file path passing by.
(Replace echo with cp to copy or mv to move the files)
Directory structure:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
After executing the command:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
âÂÂâÂÂâ 2017-09-22_cyber.gz
âÂÂâÂÂâ 2017-09-23_cyber.gz
âÂÂâÂÂâ 2017-09-24_cyber.tar
If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.
find * -path "archive" -prune -o -type f -exec
bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;
-path "archive" -pruneis excludingarchivedirectory itself.$1///_is replacing Slash/with Underscore_. The1is point to thefind_bashfirst parameter which is file path passing by.
(Replace echo with cp to copy or mv to move the files)
Directory structure:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
After executing the command:
.
âÂÂâÂÂâ 2017-09-22
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-23
âÂÂààâÂÂâÂÂâ cyber.gz
âÂÂâÂÂâ 2017-09-24
âÂÂààâÂÂâÂÂâ cyber.tar
âÂÂâÂÂâ archive
âÂÂâÂÂâ 2017-09-22_cyber.gz
âÂÂâÂÂâ 2017-09-23_cyber.gz
âÂÂâÂÂâ 2017-09-24_cyber.tar
edited Oct 4 '17 at 6:38
answered Oct 4 '17 at 5:47
ñÃÂsýù÷
15.6k92563
15.6k92563
add a comment |Â
add a comment |Â
up vote
1
down vote
Using a short shell script:
#!/bin/sh
archive='/tmp/archive'
mkdir -p "$archive"
for name in /tmp/????-??-??/*; do
dirdate=$name%/* # dirdate=$(dirname "$name")
dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")
newname="$archive/$dirdate_$name##*/"
if [ ! -e "$newname" ]; then
echo cp "$name" "$newname"
fi
done
This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.
If the new name does not exist inside the archive directory, the file is copied there.
Remove echo from the above script to actually perform the copying.
For the given example, the script will perform the following operations:
cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar
add a comment |Â
up vote
1
down vote
Using a short shell script:
#!/bin/sh
archive='/tmp/archive'
mkdir -p "$archive"
for name in /tmp/????-??-??/*; do
dirdate=$name%/* # dirdate=$(dirname "$name")
dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")
newname="$archive/$dirdate_$name##*/"
if [ ! -e "$newname" ]; then
echo cp "$name" "$newname"
fi
done
This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.
If the new name does not exist inside the archive directory, the file is copied there.
Remove echo from the above script to actually perform the copying.
For the given example, the script will perform the following operations:
cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Using a short shell script:
#!/bin/sh
archive='/tmp/archive'
mkdir -p "$archive"
for name in /tmp/????-??-??/*; do
dirdate=$name%/* # dirdate=$(dirname "$name")
dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")
newname="$archive/$dirdate_$name##*/"
if [ ! -e "$newname" ]; then
echo cp "$name" "$newname"
fi
done
This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.
If the new name does not exist inside the archive directory, the file is copied there.
Remove echo from the above script to actually perform the copying.
For the given example, the script will perform the following operations:
cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar
Using a short shell script:
#!/bin/sh
archive='/tmp/archive'
mkdir -p "$archive"
for name in /tmp/????-??-??/*; do
dirdate=$name%/* # dirdate=$(dirname "$name")
dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")
newname="$archive/$dirdate_$name##*/"
if [ ! -e "$newname" ]; then
echo cp "$name" "$newname"
fi
done
This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.
If the new name does not exist inside the archive directory, the file is copied there.
Remove echo from the above script to actually perform the copying.
For the given example, the script will perform the following operations:
cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar
edited Oct 9 '17 at 5:39
answered Oct 4 '17 at 6:34
Kusalananda
105k14209326
105k14209326
add a comment |Â
add a comment |Â