Add random number to file names
Clash Royale CLAN TAG#URR8PPP
up vote
-2
down vote
favorite
I want the new file to have the same name as the new one but with random number in the name.
For example input: sometext.txt
, output: sometext_XX.txt
and the XX
will be a random number each time.
The following code is my attempt but after the tr
command it says:
no such directory exists.
#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done
bash shell-script
add a comment |Â
up vote
-2
down vote
favorite
I want the new file to have the same name as the new one but with random number in the name.
For example input: sometext.txt
, output: sometext_XX.txt
and the XX
will be a random number each time.
The following code is my attempt but after the tr
command it says:
no such directory exists.
#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done
bash shell-script
1
Sounds like you're trying to re-inventmktemp
â steeldriver
Nov 27 '17 at 18:22
What do you mean "after thetr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anythingtr
orbash
would give. Where's$temp
set?
â ilkkachu
Nov 27 '17 at 19:24
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I want the new file to have the same name as the new one but with random number in the name.
For example input: sometext.txt
, output: sometext_XX.txt
and the XX
will be a random number each time.
The following code is my attempt but after the tr
command it says:
no such directory exists.
#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done
bash shell-script
I want the new file to have the same name as the new one but with random number in the name.
For example input: sometext.txt
, output: sometext_XX.txt
and the XX
will be a random number each time.
The following code is my attempt but after the tr
command it says:
no such directory exists.
#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done
bash shell-script
edited Nov 27 '17 at 18:03
jasonwryan
47k14127176
47k14127176
asked Nov 27 '17 at 17:52
Giorgos
1
1
1
Sounds like you're trying to re-inventmktemp
â steeldriver
Nov 27 '17 at 18:22
What do you mean "after thetr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anythingtr
orbash
would give. Where's$temp
set?
â ilkkachu
Nov 27 '17 at 19:24
add a comment |Â
1
Sounds like you're trying to re-inventmktemp
â steeldriver
Nov 27 '17 at 18:22
What do you mean "after thetr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anythingtr
orbash
would give. Where's$temp
set?
â ilkkachu
Nov 27 '17 at 19:24
1
1
Sounds like you're trying to re-invent
mktemp
â steeldriver
Nov 27 '17 at 18:22
Sounds like you're trying to re-invent
mktemp
â steeldriver
Nov 27 '17 at 18:22
What do you mean "after the
tr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr
or bash
would give. Where's $temp
set?â ilkkachu
Nov 27 '17 at 19:24
What do you mean "after the
tr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr
or bash
would give. Where's $temp
set?â ilkkachu
Nov 27 '17 at 19:24
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
Maybe something like this:
#!/bin/bash
for filename; do
base="$filename%.*"
ext="$filename##*."
tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done
The for filename
will loop over all parameters automatically. The base=
and ext=
lines separate the filename into the basename and the extension. I use $RANDOM
for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory â you'd need to do a bit more work to make this for for files in arbitrary directories.
add a comment |Â
up vote
0
down vote
How about just looping over the files? You donâÂÂt need to pass them all to the script in one go.
for file in /path/to/files/*.txt ; do mv âÂÂ$fileâ âÂÂ$file%.*âÂÂ_âÂÂ$RANDOMâÂÂ.txt ;done
I canâÂÂt test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).
$RANDOM may need curly braces too for the command to parse correctly, but canâÂÂt test it right now as I said.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Maybe something like this:
#!/bin/bash
for filename; do
base="$filename%.*"
ext="$filename##*."
tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done
The for filename
will loop over all parameters automatically. The base=
and ext=
lines separate the filename into the basename and the extension. I use $RANDOM
for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory â you'd need to do a bit more work to make this for for files in arbitrary directories.
add a comment |Â
up vote
0
down vote
Maybe something like this:
#!/bin/bash
for filename; do
base="$filename%.*"
ext="$filename##*."
tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done
The for filename
will loop over all parameters automatically. The base=
and ext=
lines separate the filename into the basename and the extension. I use $RANDOM
for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory â you'd need to do a bit more work to make this for for files in arbitrary directories.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Maybe something like this:
#!/bin/bash
for filename; do
base="$filename%.*"
ext="$filename##*."
tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done
The for filename
will loop over all parameters automatically. The base=
and ext=
lines separate the filename into the basename and the extension. I use $RANDOM
for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory â you'd need to do a bit more work to make this for for files in arbitrary directories.
Maybe something like this:
#!/bin/bash
for filename; do
base="$filename%.*"
ext="$filename##*."
tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done
The for filename
will loop over all parameters automatically. The base=
and ext=
lines separate the filename into the basename and the extension. I use $RANDOM
for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory â you'd need to do a bit more work to make this for for files in arbitrary directories.
answered Nov 27 '17 at 18:05
Andy Dalton
4,7941520
4,7941520
add a comment |Â
add a comment |Â
up vote
0
down vote
How about just looping over the files? You donâÂÂt need to pass them all to the script in one go.
for file in /path/to/files/*.txt ; do mv âÂÂ$fileâ âÂÂ$file%.*âÂÂ_âÂÂ$RANDOMâÂÂ.txt ;done
I canâÂÂt test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).
$RANDOM may need curly braces too for the command to parse correctly, but canâÂÂt test it right now as I said.
add a comment |Â
up vote
0
down vote
How about just looping over the files? You donâÂÂt need to pass them all to the script in one go.
for file in /path/to/files/*.txt ; do mv âÂÂ$fileâ âÂÂ$file%.*âÂÂ_âÂÂ$RANDOMâÂÂ.txt ;done
I canâÂÂt test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).
$RANDOM may need curly braces too for the command to parse correctly, but canâÂÂt test it right now as I said.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
How about just looping over the files? You donâÂÂt need to pass them all to the script in one go.
for file in /path/to/files/*.txt ; do mv âÂÂ$fileâ âÂÂ$file%.*âÂÂ_âÂÂ$RANDOMâÂÂ.txt ;done
I canâÂÂt test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).
$RANDOM may need curly braces too for the command to parse correctly, but canâÂÂt test it right now as I said.
How about just looping over the files? You donâÂÂt need to pass them all to the script in one go.
for file in /path/to/files/*.txt ; do mv âÂÂ$fileâ âÂÂ$file%.*âÂÂ_âÂÂ$RANDOMâÂÂ.txt ;done
I canâÂÂt test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).
$RANDOM may need curly braces too for the command to parse correctly, but canâÂÂt test it right now as I said.
edited Nov 27 '17 at 20:44
answered Nov 27 '17 at 17:56
Joe Healey
298212
298212
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%2f407335%2fadd-random-number-to-file-names%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
1
Sounds like you're trying to re-invent
mktemp
â steeldriver
Nov 27 '17 at 18:22
What do you mean "after the
tr
command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anythingtr
orbash
would give. Where's$temp
set?â ilkkachu
Nov 27 '17 at 19:24