Append all the files located in path made of wild card characters - linux

I'm trying to add "aaa" to the "ami" files without specifying full path
I tried this:
echo "aaa" >> /home/thomas/*-bbb-*/ami
instead of typing each path separate such as /home/thomas/1-bbb-2/ami and /home/thomas/1-bbb-3/ami and so on
Or should I somehow search recursively for all the files named "ami" in /home/thomas/ and add them to a variable and then append all those files in that variable?

Try using loop:
for i in `seq 150` #change 150 to your value
do
echo "aaa" > /home/thomas/1-bbb-$i/ami
done
This will work if your paths change in one place (like your example).

Save the file names in an array.
files=(/home/thomas/[0-9]*-bbb-[0-9]*/ami)
for f in "${files[#]}"; do echo 'aaa' >> "$f"; done

Related

How to create directories automatically in linux?

I am having a file named temp.txt where inside this file it contains the following content
https://abcdef/12345-xyz
https://ghifdfg/5426525-abc
I need to create a directories automatically in linux by using only th number part from each line in the file.
So the output should be something like 12345 and 5426525 directories created.
Any approach on how to do this could be helpful.
This is the code that i searched and got from internet,wherein this code, new directories will be created by the file name that starts with BR and W0 .
for file in {BR,W0}*.*; do
dir=${file%%.*}
mkdir -p "$dir"
mv "$file" "$dir"
done
Assuming each URL is of the form
http[s]://any/symbols/some_digits-some_letters
Then you indeed could use the simple prefix and suffix modifiers in shell variable expansion.
${x##*/} expands to the suffix part of x that starts after the last slash /.
${y%%-*} expands to the prefix part of y before the first -.
while read x ; do
y=${x##*/}
z=${y%%-*}
mkdir $z
done < temp.txt

ksh rename multiple files with prefix entered by user to another prefix entered by user

pretty new to scripting...
Lets say I have multiple files with multiple prefixes in current directory. I need to rename them based on user input.
Let's say I have aaa_file.dat and bbb_file.dat aaa_log.dat ccc.txt and so on.
Script will ask to enter "old prefix" and enter "new prefix" and search files with "old" prefix and mv it into name with "new" prefix.
print "enter old prefix" ;
read old ;
print "enter new prefix" ;
read new ;
find and rename part is where I get stuck..
for $old in * ;
do mv $old_* $new_* ;
done
If I enter as user aaa and the zzz my files, my result should be:
aaa_file.dat and aaa_log.dat are now named zzz_file.dat zzz_log.dat
You have some mistakes in
for $old in * ;
do
mv $old_* $new_* ;
done
When you use a for-loop, a value will be assigned to the loop variable. So drop the $ in for $old in *.
You already have assigned a value to the variable old, you do not want to overwrite it with a new value. Use a new variable like for file in * (or for file in ${old}*).
Variables can have understores in their name. $old_* is not the same as ${old}_*. Get used to using those braces in all cases except for one character variables when they are followed by a space or quote.
You are hoping for some magic, when you write ${new}_*. The variable will be extended to its value (like zzz), the shell searches for files starting with zzz_ (non found) and will try (when you hae written mv ${old}_* ${new}_*) to move all files with their name starting with aaa_ to a directory called zzz_*.
So construct a new filename from the old one with something like sed:
The new filename must be constructed from the old filename.
for file in ${old}_*; do
newfile=$(echo "${file}"| sed "s/^${old}_/${new}_/")
# Remove the word echo when the command shows the mv commands you like
echo mv "${file}" "${newfile}"
done

EASY - How can I add text into all text files in a certain directory using script?

I need to write a script that adds a certain text into all the text files in a certain directory. Here's what I have:
$1
for f in *.txt
do
echo "$2" >> *.txt <-- HERE's the problem and I don't understand why
done
...everything else works just fine.
First of all, you are missing a semicolon. Second, you have to use the variable you defined to iterate through all the text-files:
for f in *.txt; do
echo "$1" >> $f
done
I don't know for which reason you used $2. If you really need more than one argument for your script, you have to adjust the script accordingly.

Linux shell scripts for commands to update multiple folders

I have a script that I created that is just a list of commands (cp's, mkdir's, rm's, etc). What this does is basically update the contents of a bunch of folders with the contents of a source folder.
Right now I have about 15 folders to be modified with about 30 commands each. So when I need to add a folder, I need to add 30 more commands and specify that folder.
Is there a way in the script to create an array of folders to change and loop through it or something?
My script right now just contains basic commands that would normally be run in the command line, so nothing advanced.
Yes, you can do something like this:
for x in "folder1" "folder2" "folder3"; do
mkdir $x
cp foobar $x
done
Better yet, use an array to hold the folder names, e.g.
arr=("folder1" "folder2" "folder3")
for x in ${arr[*]} do
mkdir $x
cp foobar $x
done
If you have specific names following a pattern, you can probably use a loop to generate that list of names automatically.
Here is one approach:
#!/bin/bash
# This function does all you clever stuff
# $1 contains the first parameter, $2 the second and so on
function my_cmds()
{
echo $1
}
# You can loop over folders like this
for i in folder1 folder2 folder3
do
# here we call a function with the string as parameter
my_cmds $i
done
# ... or like this
folders[0]="folder0"
folders[1]="folders1"
folders[2]="folders2"
for i in "${folders[#]}"
do
my_cmds $i
done
a convenient way of initializing an entire array is the
array=( element1 element2 ... elementN )
notation.
This is similar to answers using for loops, but using a here document to store the list of folders. It's like having a data file embedded in the script.
while read -r folder <&3; do
mkdir "$folder"
# etc
done 3<<EOF
folder1
folder2
folder with space
EOF
I use file descriptor 3 for the here document in case you have commands in the body of the loop that attempt to read from standard input.

How to read the complete path till the end of the directory structure using loop in scripting

I have a following directory structure as
/home/ABCD/apple/ball/car/divider.txt, /home/ABCD this is like a root directory for my apps, I can get that easily, and from there all the sub folders may vary for every case, so I am looking for a generic program where I can extract the path through some loops
I want to extract the directory structure to a separate variable as "/home/ABCD/apple/ball/car/"
Can any one help me
2nd Example : /home/ABCD/adam/nest/mary/user.txt
variable should get the following value - "/home/ABCD/adam/nest/mary/"
Use dirname
$ dirname /home/ABCD/apple/ball/car/divider.txt
/home/ABCD/apple/ball/car
To assign to variable do
var=$(dirname /home/ABCD/apple/ball/car/divider.txt)
echo "$var"
No spaces before and after the =
if the ending slash / is required, you could pick one:
kent$ echo "/home/ABCD/adam/nest/mary/user.txt"|grep -Po '.*/'
/home/ABCD/adam/nest/mary/
or
kent$ echo "/home/ABCD/adam/nest/mary/user.txt"|sed -r 's#(.*/).*#\1#'
/home/ABCD/adam/nest/mary/
or
kent$ echo $(dirname /home/ABCD/adam/nest/mary/user.txt)"/"
/home/ABCD/adam/nest/mary/

Resources