Renaming files from a list using a shell script - shell

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

Related

Shell Scripting. How to copy one file to all the folders in PWD

sorry i am new to shell scripting
I have few folders in PWD. i need to copy one file to all the folders in PWD. FIle is also stored in present working directory . How can i do that?
maybe something like:
for d in */; do cp -v ./file "$d/"; done

Copy file without file extension to new folder and rename it

I just during the weekend decided to try out zsh and have a bit of fun with it. Unfortunately I'm an incredible newbie to shell scripting in general.
I have this folder with a file, which filename is a hash (4667e85581f80b6936f8811f0a7493c70eae4ee7) without a file-extension.
What I would like to do is copy this file to another folder and rename it to "screensaver.png".
I've tried with the following code:
#!/usr/bin/zsh
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva"
DEST_FOLDER="/Library/Desktop Pictures/Kuvva/$USERNAME/screensaver.png"
for wallpaper in ${KUVVA_CACHE}; do
cp -f ${wallpaper} ${DEST_FOLDER}
done
This returns the following error:
cp: /Users/Morten/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva is a directory (not copied).
And when I try to echo the $wallpaper variable instead of doing "cp" then it just echo's the folder path.
The name of the file changes every 6 hour, which is why I'm doing the for-loop. So I never know what the name of the file will be, but I know that there's always only ONE file in the folder.
Any ideas how I can manage to do this? :)
Thanks a lot!
Morten
It should work with regular filename expansion (globbing).
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva/"
And then copy
cp -f ${KUVVA_CACHE}/* ${DEST_FOLDER}
You can add the script to your crontab so it will be run at a certain interval. Edit it using 'crontab -e' and add
30 */3 * * * /location/of/your/script
This will run it every third hour. First digit is minutes. Star indicates any. Exit the editor by pressing the escape-key, then shift+: and type wq and press enter. These vi-commands.
Don't forget to 'chmod 0755 file-name' the script so it becomes executable.
Here is the script.
#!/bin/zsh
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva"
DEST_FOLDER="/Library/Desktop Pictures/Kuvva/$USERNAME/screensaver.png"
cp "${KUVVA_CACHE}/"* "${DEST_FOLDER}"

Shell Script to rename file with string from another filename

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

Bash script to execute only on new files in directory

I'm currently running a Bash script called (log2csv) that runs against a specified .log file. Sitting in the desired directory I can type in terminal:
log2csv Red1_1.log
This will create Red1_1.csv
This is my current bash script:
#!/bin/bash
for path
do
base=$(basename "$path")
noext="${base/.log}"
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done
This script is actually running a perl script on the specified log and putting the results in a CSV output.
I can alternatively run in terminal:
log2csv *.log
This will run the script against all .log files in the current directory and create .csv files for every one.
What I would like the script to do is only run on .log files that haven't had .csv files created for them. After doing some research I think I possibly can use inotifywait to achieve this, but I'm unsure how to make this work in my script? I also have read that this may be an issue if you overwrite a file. Any help or ideas would be most appreciated!
What I would like the script to do is only run on .log files that haven't had .csv files created for them.
Simply skip those .log files whose corresponding .csv files already exist:
for path
do
base=$(basename "$path")
noext="${base/.log}"
[ -e "${noext}.csv" ] && continue # <---------------
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done

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`

Resources