View previous topic :: View next topic |
Author |
Message |
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 693
|
Posted: Sun Feb 10, 2019 7:41 am Post subject: how to rename files in random names |
|
|
there is a folder with files. how can i rename this files to files with random names from a-z and 0-9, with 24...32 characters? |
|
Back to top |
|
|
fedeliallalinea Administrator
Joined: 08 Mar 2003 Posts: 30837 Location: here
|
Posted: Sun Feb 10, 2019 8:08 am Post subject: |
|
|
For create random file name you can use command
Code: | $ cat /dev/urandom | tr -cd 'a-z0-9' | head -c 32 |
_________________ Questions are guaranteed in life; Answers aren't. |
|
Back to top |
|
|
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 693
|
Posted: Sun Feb 10, 2019 10:20 am Post subject: |
|
|
how can i do it so that the length is various, between 24...32 characters ?
and the next, how can i run this command in a folder with files that they are all renamed automatically?
(sorrie i am a newbie with bash scripts) |
|
Back to top |
|
|
fedeliallalinea Administrator
Joined: 08 Mar 2003 Posts: 30837 Location: here
|
Posted: Sun Feb 10, 2019 10:32 am Post subject: |
|
|
SarahS93 wrote: | how can i do it so that the length is various, between 24...32 characters ? |
Code: | cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1) |
SarahS93 wrote: | and the next, how can i run this command in a folder with files that they are all renamed automatically?
(sorrie i am a newbie with bash scripts) |
Run (this show only how file is renamed)
Code: | $ cd dir_with_file_to_rename
$ for file in *; do echo "${file}" $(cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1)); done |
then when you see that all is correct change echo with mv command
EDIT: this command rename also directories if exists _________________ Questions are guaranteed in life; Answers aren't.
Last edited by fedeliallalinea on Sun Feb 10, 2019 11:23 am; edited 1 time in total |
|
Back to top |
|
|
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 693
|
Posted: Sun Feb 10, 2019 11:19 am Post subject: |
|
|
great, thank you very much! |
|
Back to top |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3920 Location: Hamburg
|
Posted: Sun Feb 10, 2019 2:10 pm Post subject: |
|
|
fedeliallalinea wrote: |
Code: | cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1) |
| Cool idea. What's about using "cut" instead of "head" ? |
|
Back to top |
|
|
NeddySeagoon Administrator
Joined: 05 Jul 2003 Posts: 54096 Location: 56N 3W
|
Posted: Sun Feb 10, 2019 3:47 pm Post subject: |
|
|
fedeliallalinea,
That does not test for unique random names.
With 24+ character names, its unlikely to be a real world problem. :) _________________ Regards,
NeddySeagoon
Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21489
|
Posted: Sun Feb 10, 2019 5:20 pm Post subject: |
|
|
You can solve the uniqueness problem and simplify by using mktemp to roll a random unused name, reserve it, and then you overwrite it with the mv. This also avoids using cat just to feed tr. This will also partially solve the directory problem, since the file made by mktemp cannot be overwritten by a directory. Code: | XX=XXXXXXXX
find . -maxdepth 1 -type f -print0 | while read -d '' srcname; do
echo mv "$srcname" "$(mktemp -u -p . $XX$XX$XX${XX:0:$(($RANDOM % 8))})"
done
| This shows what would be done. To make this live, remove the -u and the echo. For simplicity, this version allows uppercase characters too. If lowercase-only is a hard requirement, you could save the name into a bash variable and lowercase it there, but then you don't have the uniqueness guarantee. If you do that, you should leave mktemp in dry-run mode so that it doesn't leave behind names with uppercase letters. |
|
Back to top |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3920 Location: Hamburg
|
Posted: Sun Feb 10, 2019 7:15 pm Post subject: |
|
|
Hu wrote: | You can solve the uniqueness problem and simplify by using mktemp | nit-picking, but I learned, that mktemp is not an atomic operation under linux kernel, whereas mktemp -d is it! |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21489
|
Posted: Sun Feb 10, 2019 7:47 pm Post subject: |
|
|
What do you mean by not atomic? strace says that mktemp passed O_RDWR|O_CREAT|O_EXCL, so either the create succeeds and gives a new file or it fails and mktemp can try again. Yes, it takes multiple system calls, and that part is not atomic, but I don't see how that race can do anything to violate mktemp's promise of a unique file. mktemp -d likewise requires multiple system calls, so it appears to be just as (non-)atomic as regular mktemp. |
|
Back to top |
|
|
toralf Developer
Joined: 01 Feb 2004 Posts: 3920 Location: Hamburg
|
Posted: Sun Feb 10, 2019 8:46 pm Post subject: |
|
|
Hu wrote: | What do you mean by not atomic? strace says that mktemp passed O_RDWR|O_CREAT|O_EXCL, so either the create succeeds and gives a new file or it fails and mktemp can try again. Yes, it takes multiple system calls, and that part is not atomic, but I don't see how that race can do anything to violate mktemp's promise of a unique file. mktemp -d likewise requires multiple system calls, so it appears to be just as (non-)atomic as regular mktemp. | This http://www.linux-magazin.de/ausgaben/2012/05/bash-bashing/2/ explains (in german) why mkdir is better than a simple file. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21489
|
Posted: Sun Feb 10, 2019 9:00 pm Post subject: |
|
|
mktemp is not a simple file created-or-opened by the shell. It is a file guaranteed to have been created new by mktemp. Yes, using file existence tests / touch as shown in the article is unreliable, because those do not use O_EXCL. |
|
Back to top |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Mon Feb 11, 2019 12:10 am Post subject: |
|
|
I've been curious what the application is, that requires a folder full of files to be renamed randomly. _________________ Many think that Dilbert is a comic. Unfortunately it is a documentary. |
|
Back to top |
|
|
fedeliallalinea Administrator
Joined: 08 Mar 2003 Posts: 30837 Location: here
|
Posted: Mon Feb 11, 2019 6:52 am Post subject: |
|
|
NeddySeagoon wrote: | That does not test for unique random names.
With 24+ character names, its unlikely to be a real world problem. |
You right but can use mv -i for prevent overwriting or check the randon name generated with a little test
Code: | for file in *; do
while true; do
RANDOM_NAME=$(cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1));
if [ ! -f ${RANDOM_NAME} ]; then
echo "${file}" "${RANDOM_NAME}"
break;
fi
done
done |
_________________ Questions are guaranteed in life; Answers aren't. |
|
Back to top |
|
|
Ant P. Watchman
Joined: 18 Apr 2009 Posts: 6920
|
Posted: Mon Feb 11, 2019 8:15 am Post subject: |
|
|
If you're okay with 36 character names:
Code: | for nm in *; do mv -vn "$nm" $(</proc/sys/kernel/random/uuid); done |
|
|
Back to top |
|
|
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 693
|
Posted: Wed Mar 13, 2019 8:08 pm Post subject: |
|
|
Code: | for file in *; do echo "${file}" $(cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1)); done |
works fine.
this is for an aprilfool, so i need a way to save the original filenames with the new names, how does this work? |
|
Back to top |
|
|
NeddySeagoon Administrator
Joined: 05 Jul 2003 Posts: 54096 Location: 56N 3W
|
Posted: Wed Mar 13, 2019 8:11 pm Post subject: |
|
|
SarahS93,
This prank might be mistaken for ransomware. Are you sure its a good idea? _________________ Regards,
NeddySeagoon
Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail. |
|
Back to top |
|
|
fedeliallalinea Administrator
Joined: 08 Mar 2003 Posts: 30837 Location: here
|
Posted: Wed Mar 13, 2019 8:19 pm Post subject: |
|
|
Code: | rm original_names.txt
for file in *; do
while true; do
RANDOM_NAME=$(cat /dev/urandom | tr -cd 'a-z0-9' | head -c $(shuf -i 24-32 -n 1));
if [ ! -f ${RANDOM_NAME} ]; then
echo "${file}" "${RANDOM_NAME}" >> original_names.txt
echo "${file}" "${RANDOM_NAME}";
break;
fi
done
done |
Test if work change second echo with mv _________________ Questions are guaranteed in life; Answers aren't. |
|
Back to top |
|
|
NeddySeagoon Administrator
Joined: 05 Jul 2003 Posts: 54096 Location: 56N 3W
|
Posted: Wed Mar 13, 2019 8:58 pm Post subject: |
|
|
fedeliallalinea,
... and the undo magical incantation is ... _________________ Regards,
NeddySeagoon
Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail. |
|
Back to top |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Wed Mar 13, 2019 10:00 pm Post subject: |
|
|
Why are we helping someone write what appears to be ransomware? I cannot think of a legitimate reason to ever need to randomly rename files. Doesn't mean there isn't one (maybe my imagination isn't good enough). But, absent a compelling stated purpose, I would certainly not want my name associated with this. To me, this smells like either someone's homework assignment, or a poor attempt at writing malware.
I sincerely hope I'm proven wrong. _________________ Many think that Dilbert is a comic. Unfortunately it is a documentary. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21489
|
Posted: Thu Mar 14, 2019 1:44 am Post subject: |
|
|
Akkara wrote: | Why are we helping someone write what appears to be ransomware? I cannot think of a legitimate reason to ever need to randomly rename files. Doesn't mean there isn't one (maybe my imagination isn't good enough). But, absent a compelling stated purpose, I would certainly not want my name associated with this. To me, this smells like either someone's homework assignment, or a poor attempt at writing malware.
I sincerely hope I'm proven wrong. | I chimed in because earlier posts used shell in grossly suboptimal ways. I'm so used to people asking for useless things on the basis that they think it will solve their problem that I didn't think of that possibility. Now that this possibility has been pointed out, I'm out, barring a better explanation than "April Fool's prank", particularly given the thread's start date versus April 1st. |
|
Back to top |
|
|
fedeliallalinea Administrator
Joined: 08 Mar 2003 Posts: 30837 Location: here
|
Posted: Thu Mar 14, 2019 6:47 am Post subject: |
|
|
Hu wrote: | I'm so used to people asking for useless things on the basis that they think it will solve their problem that I didn't think of that possibility. |
++ _________________ Questions are guaranteed in life; Answers aren't. |
|
Back to top |
|
|
krinn Watchman
Joined: 02 May 2003 Posts: 7470
|
Posted: Thu Mar 14, 2019 11:51 am Post subject: |
|
|
that won't be any fun, if you do such thing to my files, i won't welcome you with a smile, more with a hammer.
the first reaction from people victim of things like that, is deleting the offending files in order to "stop spreading".
what will you do once your stupid idea (because it's an idea, never i would consider this a joke) has made your friends remove their precious files? |
|
Back to top |
|
|
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 693
|
Posted: Thu Mar 14, 2019 12:38 pm Post subject: |
|
|
guys, that has nothing todo with any kind of ransomware.
i plan an april fool to my brothers computer.
later,i will use this way of crypting filenames to post some stuff to the usenet.
i can rename files back by hand, but is there a way to let a script do this?
i am bad in bash shell scripts, so i ask here, and i thank you guys for your help! |
|
Back to top |
|
|
NeddySeagoon Administrator
Joined: 05 Jul 2003 Posts: 54096 Location: 56N 3W
|
Posted: Thu Mar 14, 2019 7:09 pm Post subject: |
|
|
SarahS93,
One word of advice. Don't. _________________ Regards,
NeddySeagoon
Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail. |
|
Back to top |
|
|
|