Shell Script to rename file with string from another filename - bash

I have been stuck with a task where I need to rename a file with a substring from another filename and then move it to a directory.
What I want to achieve here is as below
I have a file named dummy.txt and another file named 20150416demo.xml
What I want is the file abc.txt to be renamed with the date from the xml file i.e. dummy20150416.txt
The final step is to move it to a different location. Appreciate you responses
Thanks

To rename/concatenate and move according to what you want you can use this script:
#!/bin/bash
file="20150416demo.xml"
date=${file:0:8}
mv abc.txt /destination/path/dummy$date.txt

Related

how to create a zip file of the cd in bash script

i want to create the zip file of the current working directory in bash script. AND Redirect the stdout of zip command to a file called zip-output.txt.
i have a current workind directory called "music" and my script populates it with a lot of content. thus after it is populated i want to convert it to "music.zip" . but the contents of the "music" directory shouldn't be altered or zipped.
/Users/xyz/Downloads/music
this is the path to the cd "music" if need be and the file "zip-output.txt" could be created at the path "/Users/xyz/Downloads"
ik this is easy but i am new. please guide me
Do you mean something like this?
zip -r music.zip music/ | tee zip-output.txt

Renaming files from a list using a shell script

I would like to rename files from a list. This is the file format:
Original name
New name
000000000000402a.ogg
Dth_BrkNck_FOD_MALE_MALE_001
000000000000402c.ogg
Dth_BrkNck_FOD_MALE_MALE_002
000000000000402e.ogg
Dth_BrkNck_FOD_MALE_MALE_003
0000000000004030.ogg
Dth_BrkNck_FOD_MALE_MALE_004
I'm kinda new to shell scripts, so any help will be appreciated. Thank you.
Seems like there is not direct connection b/w Original file name and New file name, To rename a file,
mv OriginalName NewName
one way is to repeat the step for multiple files, ( we can create all commands in a notepad and paste it altogether)
or we can put it in a shell script , say renamer.sh as below and execute it,
cat renamer.sh
mv 000000000000402a.ogg Dth_BrkNck_FOD_MALE_MALE_001
mv 000000000000402c.ogg Dth_BrkNck_FOD_MALE_MALE_002
mv 000000000000402e.ogg Dth_BrkNck_FOD_MALE_MALE_003
mv 0000000000004030.ogg Dth_BrkNck_FOD_MALE_MALE_004
to execute :- sh renamer.sh

creating a copy of a file with different name in bash

I have a filename stored in $file.
I need to delete some contents from that file for some calculation. But the original file should not be modified. So I am looking for a way to create a duplicate copy of that file to modify, and then delete it later. Is there a way to create a file with command like "$filename+2" or something like that? How do I go about doing this?
Use cp as already mentioned but consider using mktemp to create the temp filename.

open a folder from a name in txt file BASH

I have a text file fold.txt that contains one line fold_nam:
$ cat fold.txt
fold_nam
This name in the the text file is an output that was created during a program run of a folder's name that now contains other files that I need to work with.
I am writing a big script and now I need to enter this folder and I need to get the name from the text file. I tried several things but cannot really work it out.
There's no need to use cat:
cd $(<fold.txt)
If you want to read the line into a variable: read -r folder_name < fold.txt
You should be able to do this:
cd $(cat fold.txt)
or
cd `cat fold.txt`

How can I display a text file within a bash script?

Im trying to display a text file in a directory within the folder my script is in. I tried things like:
mypath=`realpath $0`
FILE="$realpath/Folder/Text.txt"
cat $FILE
And
FILE="$PWD/Folder/Text.txt"
cat $FILE
but they include the name of the file instead of just the its running from. I also want it to work with symbolic links.
cat "$(dirname -- "$0")/Folder/Text.txt"
That is, send the file Text.txt in the directory Folder below the directory where this script is located to standard output.
This will work with symlinks. And yes, all the quotes are significant.
You can do:
cat "./Folder/Text.txt"
the period denotes the current folder you are in. a double period "../Folder/Text.txt" denotes a folder up in the directory tree.

Resources