Generate a random filename in unix shell
Assuming you are on a linux, the following should work:
cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32
This is only pseudo-random if your system runs low on entropy, but is (on linux) guaranteed to terminate. If you require genuinely random data, cat /dev/random
instead of /dev/urandom
. This change will make your code block until enough entropy is available to produce truly random output, so it might slow down your code. For most uses, the output of /dev/urandom
is sufficiently random.
If you on OS X or another BSD, you need to modify it to the following:
cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32
Simple shell script that creates a dir with a random name
try just:
#!/bin/bash
DATAOGGI=$(date +"%Y-%m-%d")
RANDOM_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
mkdir "${DATAOGGI}${RANDOM_STRING}"
apart from fact that it is not necessary in this example echo -n
AFAIK has very inconsistent behavior and it is advised to use printf
instead
Create a random name for the complete script
declare your own variable first:
myrandom=$RANDOM
for i in $(ls -latr sample_*.csv); do
paste -d, $i >> out_${myrandom}.csv;
done
sed 's/^|$/\x27/g' out_${myrandom}.csv | paste -d, > merged_t${myrandom}.csv
How can I select random files from a directory in bash?
Here's a script that uses GNU sort's random option:
ls |sort -R |tail -$N |while read file; do
# Something involving $file, or you can leave
# off the while to just get the filenames
done
Related Topics
How to Start/Restart/Stop Apache Server on Linux as Non-Root User
Shell Script to Delete Files When Disk Is Full
Opening New Gnome-Terminal (V3.28+) with Multiple Tabs and Different Commands
Get the Pid of a Running Playbook for Use Within the Playbook
How to Pass a Complete Argument List in Bash While Keeping Mulitword Arguments Together
How to Get the Exit Status of the First Command in a Pipe
Fsync, Sync: Does It Really Do What Its Supposed To
Why Would the Elf Header of a Shared Library Specify Linux as the Osabi
Unable to Set Variable in Case Statement Bash
Making Proprietary Elf Binaries Portable on Linux
How to Execute 32-Bit Code in 64-Bit Process by Doing Mode-Switching
How to to Delete a Line Given with a Variable in Sed
Rsync, 'Uid/Gid Impossible to Set' Cases Cause Future Hard Link Failure, How to Fix
Wget: Unsupported Scheme on Non-Http Url
Mathematical Expression Result Assigned to a Bash Variable
How to Modify Eip's Tracee Forked Procee
Screen Command Disable the Control Key Ctrl-A to Use It in Vim
Will Data Written via Write() Be Flushed to Disk If a Process Is Killed