Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to rename files in random names
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
SarahS93
Guru
Guru


Joined: 21 Nov 2013
Posts: 460

PostPosted: Sun Feb 10, 2019 7:41 am    Post subject: how to rename files in random names Reply with quote

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
View user's profile Send private message
fedeliallalinea
Bodhisattva
Bodhisattva


Joined: 08 Mar 2003
Posts: 21549
Location: here

PostPosted: Sun Feb 10, 2019 8:08 am    Post subject: Reply with quote

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
View user's profile Send private message
SarahS93
Guru
Guru


Joined: 21 Nov 2013
Posts: 460

PostPosted: Sun Feb 10, 2019 10:20 am    Post subject: Reply with quote

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
View user's profile Send private message
fedeliallalinea
Bodhisattva
Bodhisattva


Joined: 08 Mar 2003
Posts: 21549
Location: here

PostPosted: Sun Feb 10, 2019 10:32 am    Post subject: Reply with quote

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
View user's profile Send private message
SarahS93
Guru
Guru


Joined: 21 Nov 2013
Posts: 460

PostPosted: Sun Feb 10, 2019 11:19 am    Post subject: Reply with quote

great, thank you very much!
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3652
Location: Hamburg

PostPosted: Sun Feb 10, 2019 2:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42842
Location: 56N 3W

PostPosted: Sun Feb 10, 2019 3:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 13601

PostPosted: Sun Feb 10, 2019 5:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3652
Location: Hamburg

PostPosted: Sun Feb 10, 2019 7:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 13601

PostPosted: Sun Feb 10, 2019 7:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3652
Location: Hamburg

PostPosted: Sun Feb 10, 2019 8:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 13601

PostPosted: Sun Feb 10, 2019 9:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Akkara
Administrator
Administrator


Joined: 28 Mar 2006
Posts: 6631
Location: &akkara

PostPosted: Mon Feb 11, 2019 12:10 am    Post subject: Reply with quote

I've been curious what the application is, that requires a folder full of files to be renamed randomly.
_________________
Isn't it odd that no job openings ever list being potty-trained as one of the requirements?
Back to top
View user's profile Send private message
fedeliallalinea
Bodhisattva
Bodhisattva


Joined: 08 Mar 2003
Posts: 21549
Location: here

PostPosted: Mon Feb 11, 2019 6:52 am    Post subject: Reply with quote

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
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 5644

PostPosted: Mon Feb 11, 2019 8:15 am    Post subject: Reply with quote

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
View user's profile Send private message
SarahS93
Guru
Guru


Joined: 21 Nov 2013
Posts: 460

PostPosted: Wed Mar 13, 2019 8:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42842
Location: 56N 3W

PostPosted: Wed Mar 13, 2019 8:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
fedeliallalinea
Bodhisattva
Bodhisattva


Joined: 08 Mar 2003
Posts: 21549
Location: here

PostPosted: Wed Mar 13, 2019 8:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42842
Location: 56N 3W

PostPosted: Wed Mar 13, 2019 8:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
Akkara
Administrator
Administrator


Joined: 28 Mar 2006
Posts: 6631
Location: &akkara

PostPosted: Wed Mar 13, 2019 10:00 pm    Post subject: Reply with quote

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.
_________________
Isn't it odd that no job openings ever list being potty-trained as one of the requirements?
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 13601

PostPosted: Thu Mar 14, 2019 1:44 am    Post subject: Reply with quote

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
View user's profile Send private message
fedeliallalinea
Bodhisattva
Bodhisattva


Joined: 08 Mar 2003
Posts: 21549
Location: here

PostPosted: Thu Mar 14, 2019 6:47 am    Post subject: Reply with quote

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
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7006

PostPosted: Thu Mar 14, 2019 11:51 am    Post subject: Reply with quote

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
View user's profile Send private message
SarahS93
Guru
Guru


Joined: 21 Nov 2013
Posts: 460

PostPosted: Thu Mar 14, 2019 12:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42842
Location: 56N 3W

PostPosted: Thu Mar 14, 2019 7:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum