I'm trying to make the terminal make a bunch of files (it's on a loop), with random names, random size, random data, in random directories. By random directories I mean they could be anywhere on the computer
maybe a starting point is this script
#!/bin/bash
for i in {1..5}
do
# Create random file
#
data=$(od -vAn -N4 -tu4 < /dev/urandom)
random_file=$(mktemp)
echo "$data" > "$random_file"
echo "Created $random_file file, with data=$data"
# Create random directory
#
random_dir=$(mktemp -d)
echo "Created $random_dir directory"
done
output:
Created /tmp/tmp.Toet7Wn7bz file, with data= 4221696069
Created /tmp/tmp.kc9OLaX84U directory
Created /tmp/tmp.S2OQ8Wh6Iz file, with data= 172066121
Created /tmp/tmp.d1oFQ4DINx directory
Created /tmp/tmp.wGl42vpRLc file, with data= 3188097801
Created /tmp/tmp.xbAhMUFAZi directory
Created /tmp/tmp.4dI5M7amYL file, with data= 1085037810
Created /tmp/tmp.vAMAg39pjK directory
Created /tmp/tmp.AXP6yztDMj file, with data= 2348013838
Created /tmp/tmp.Qc5kyZtmnn directory
It generates 5 random files storing a random number and 5 random directories.
This is an initial idea only (-> the directories and files are not nested, etc.) but I guess this should be possible to extend this initial script.
Related
I've done a small amount of bash scripting. Mostly modifying a script to my needs.
On this one I am stumped.
I need a script that will read a sub-folder name inside a folder and make a numbered list of folders based on that sub-folder name.
Example:
I make a folder named “Pictures”.
Then inside I make a sub-folder named “picture-set”
I want a script to see the existing sub-folder name (picture-set) and make 10 more folders with sequential numbers appended to the end of the folder names.
ex:
folder is: Pictures
sub-folder is: picture-set
want to create:
“picture-set-01”
“picture-set-02”
“picture-set-03”
and so forth up to 10. Or a number specified in the script.
The folder structure would look like this:
/home/Pictures/picture-set
/home/Pictures/picture-set-01
/home/Pictures/picture-set-02
/home/Pictures/picture-set-03
... and so on
I am unable to tell the script how to find the base folder name to make additional folders.
ie: “picture-set”
or a better option:
Would be to create a folder and then create a set of numbered sub-folders based on the parent folder name.
ex:
/home/Songs - would become:
/home/Songs/Songs-001
/home/Songs/Songs-002
/home/Songs/Songs-003
and so on.
Please pardon my bad formatting... this is my first time asking a question on a forum such as this. Any links or pointers as to proper formatting is welcome.
Thanks for the help.
Bash has a parameter expansion you can use to generate folder names as arguments to the mkdir command:
#!/usr/bin/env bash
# Creates all directories up to 10
mkdir -p -- /home/Songs/Songs-{001..010}
This method is not very flexible if you need to dinamically change the range of numbers to generate using variables.
So you may use a Bash for loop and print format the names with desired number of digits and create each directory in the loop:
#!/usr/bin/env bash
start_index=1
end_index=10
for ((i=start_index; i<=end_index; i++)); do
# format a dirpath with the 3-digits index
printf -v dirpath '/home/Songs/Songs-%03d' $i
mkdir -p -- "$dirpath"
done
# Prerequisite:
mkdir Pictures
cd Pictures
# Your script:
min=1
max=12
name="$(basename "$(realpath .)")"
for num in $(seq -w $min $max); do mkdir "$name-$num"; done
# Result
ls
Pictures-01 Pictures-03 Pictures-05 Pictures-07 Pictures-09 Pictures-11
Pictures-02 Pictures-04 Pictures-06 Pictures-08 Pictures-10 Pictures-12
I'm new in Bash and I have a list of names of directories stored in an excel file. I'd like to find those directories (they are located in different location at the computer) and to copy from each directory specific files (list of 4 files that ends with specific endings) to a remote computer.
For examples:
For a name of directory at the excel sheet - "NA123", I'd like to find it and copy it's partial content to a remote computer, for example copy the files: samples-sheet.csv, toInfo.xml, newfiles.gz, todo.csv to the remote computer, under a folder name "NA123".
How do I begin to do that?
****Editing to give an example of how it needs to be*****
A short example of the csv is as below:
A
1 14RD00129_TS1_01
2 SD-2015-06_01
3 US-005
4 RA99
All the names at the csv are directories that can be found under /home/bella/samples under 3 different folders: some will be at /home/bella/samples/gruop_1, some at:/home/bella/samples/gruop_2, and some at:/home/bella/samples/gruop_3
So first I need to iterate through the csv file, to locate the match directory at my computer, then I need to copy 4 specific files to a remote computer with the same name of directory. Hope this is clearer...
I guess you CSV file should only consist of directory names then, since there's only one column. I assume there is no header line in the CSV (A in your example) and no line number. You can take this as a starting point:
samples='/home/bella/samples'
while IFS= read -r line; do
dir=$(find "$samples"/gruop_{1..3} -type d -name "$line")
scp "$dir"/{samples-sheet.csv,toInfo.xml,newfiles.gz,todo.csv} \
user#host.com:"/path/to/$line"
done < 'file.csv'
Basically, you could do something like:
# create the directory on the remote:
ssh remote-ip 'mkdir -p NA123'
# copy the files to the remote in the directory just created
for f in samples-sheet.csv toInfo.xml newfiles.gz todo.csv; do scp $f remote-ip:NA123/; done
I have a directory structure like so
/dir01/dir02/files
I want to copy the first file in dir02 onto a separate drive and into a directory with the same name as dir01
I wrote the following script
while [ "${*}" != "" ] ; do
INPUT="${1}"
FOLDER="${INPUT}"/*DPX
TARGET_FOLDER="/Users/user/Desktop/folder"/$(basename "${INPUT}")
for file in "${FOLDER}"; do
echo cp "$file" "${TARGET_FOLDER}"
break 1
done
done
Here INPUT is dir01 , FOLDER is dir02 and TARGET_FOLDER is the new directory with the same name as dir02 I want the file to copy to.
When I run the script it looks for a folder named *DPX in the INPUT path, which doesn't exist. There are many folders in the INPUT directory named *DPX and I want it to pull the first file from all of them.
Try replacing your for with:
for file in "$INPUT"/*DPX/*
Notes:
Your version is looking for a file called *DPX because ${FOLDER} is quoted on the for line.
for f in "$dir" will execute the for loop once, with f=$dir. To look for files under $dir, you need another /*.
Also, you want a shift before the last done.
I'm writing a unix shell script that sorts data in ten subdirectories (labelled 1-10) of the home directory. In each subdirectory, the script needs to rename the files hehd.output and fort.hehd.time, as well as copy the file hehd.data to a .data file with a new name.
What I'd like it to do is rename each of these files in the following format:
AA.BB.CC
Where
AA = a variable in the hehd.data file within the subdirectory containing the file
BB = the name of the subdirectory containing the file (1-10)
CC = the original file name
Each subdirectory contains an hehd.data file, and each hehd.data file contains the string ij0=AA, where AA represents the variable I want to use to rename the files in the same subdirectory.
For example: When run, the script should search /home/4/hehd.data for the string ij0=2, then move /home/4/hehd.output to /home/4/2.4.hehd.output.
I'm currently using the grep command to have the script search for the string ij0=* and copy it to a new text file within the subdirectory. Next, the string ij0= is deleted from the text file, and then its contents are used to rename all target files in the same subdirectory. The last line of the shell script deletes the text file.
I'm looking for a better way to accomplish this, preferably such that all ten subdirectories can be sorted at once by the same script. My script seems incredibly inefficient, and doesn't do everything that I want it to by itself.
How can I improve this?
Any advice or suggestions would be appreciated; I'm trying to become a better computer user and that means learning better ways of doing things.
Try this:
fromdir=/home
for i in {1..10};do
AA=$(sed 's/ij0=\([0-9]*\)/\1/' "$fromdir/$i/hehd.data")
BB="$i"
for f in "$fromdir/$i/"*;do
CC="${f##*/}"
if [[ "$CC" = "hehd.data" ]]; then
echo cp "$f" "$fromdir/$i/$AA.$BB.$CC"
else
echo mv "$f" "$fromdir/$i/$AA.$BB.$CC"
fi
done
done
It loops over directories using Bash sequence {1..10].
In each directory, with the sed command the ij0 value is assigned to AA variable, the directory name is assigned to BB.
In the file loop, if the file is hehd.data it's copied, else it's renamed with the new name.
You can remove the echo before cp and mv commands if the output meets your needs.
I have a set of files that I would like to rename by using new filenames stored in a txt file. My original files:
7170M
7172M
7187P
7192N
7198P
I would like to add to each filename specific new names so that the above files become:
1956_26_7170M
1962_12_7172M
1989_32_7187P
1986_22_7192N
1943_13_7198P
I created a document new_names.txt containing new filenames and tried the following.
for f in *.txt; do mv "$f" "$new_name"; done < new_names.txt
But it changes only the first filename.
You can only store the new parts of the names in the file (provided you'll process the files in the sorted order):
1956_26
1962_12
1989_32
1986_22
1943_13
Then, iterate over the files, read one line for each of them:
for file in 7* ; do
read new
mv "$file" "$new"_"$file"
done < new_names.txt
Your main problem was than for ... in doesn't read from a file, it iterates over a list given after in. In your case, the list only had one member: new_names.txt. Also, You didn't populate $new_name anywhere.