Rename multiple values in file name - bash

I have a lot of files like these:
nsh_nyi.mp4
ott_pit.mp4
chi_wsh.mp4
nyr_mtl.mp4
dal_tbl.mp4
stl_edm.mp4
min_ari.mp4
I would like to rename all those files with onliner like so:
nsh_nyi.mp4 becomes nashville predators - new york islanders.mp4
ott_pit.mp4 becomes ottawa senators - pittsburgh penguins.mp4
Is it possible to with one command in linux, using awk,rename, sed etc?
I tried to bulk rename those files like so:
rename s/nsh/nashville\ predators/g
It will replace one part of the file, but not the second part: nyi
Of course i have all those club names in one file.
The result is:
nsh_nyi.mp4
Can't rename nsh_nyi.mp4 nsh_New-york-islanders.mp4: No such file or directory
ott_pit.mp4
Can't rename ott_pit.mp4 ott_Pittsburgh-penguins.mp4: No such file or directory

You can replace multiple patterns by listing them all separated by semi-colon as below.
rename 's/nsh/nashville\ predators/g;s/ott/ottawa\ senators/g'
Reference

Related

Replace files in multiple folders, whose name includes destination folder name

I have very little experience with the command line and I'm trying to do something very complicated (to me).
I have a directory with A LOT of subfolders and files in them. All file names contain the parent folder name, e.g.:
Folder1
data_Folder1.csv
other_file_Folder1.csv
Folder2
data_Folder2.csv
other_file_Folder2.csv
In another folder (all in one directory), I have a new version of all the data_FolderX.csv files and I need to replace them in the original folders. I cannot give them another name because of later analyses. Is there a way to replace the files in the original folders with the new version, in the command line?
I tried this Replacing a file into multiple folders/subdirectories but didn't work for me. Given that I have many .csv files in the derectories, I don't want to replace them all, so I don't think I should do it based on the file extension. I would also like to note that the name "FolderX" contains several other _, so in principal, I want to replace the .csv file starting with data in the FolderX.
Can anyone help?
Thanks in advance!

how do i combine txt file from a list of file emplacement

i have a problem, i used "everything" to extract every txt file from a specific directory so that i can merge them. But on emeditor i don't find a way to merge file from a list of localisation.
Here what the everything file look like:
E:\Main directory\subdirectory 1\file.txt
E:\Main directory\subdirectory 2\file.txt
E:\Main directory\subdirectory 3\file.txt
E:\Main directory\subdirectory 4\file.txt
The list goes over 40k location. is there a way to use a program to read all the location in the text file and combine them ?
Also, the subdirectory has other txt file that i don't want to so i can't just merge all txt file from the main. Another thing is that there are variation of the "file.txt" like "Files.txt" for example.

Shell script - replace a string in the all the files in a directory based on file name

I want to replace a constant string in multiple files based on the name of the file.
Example:
In a directory I have many files named like 'X-A01', 'X-B01', 'X-C01'.
In each file there is a string 'SS-S01'.
I want to replace string 'SS-S01' in the first file with 'X-A01', second file with 'X-B01' and third file with 'X-C01'.
Please help me how can we do it as I have hundreds of files like this and do not want to manually edit all files one by one.
Remember to back up your files(!) before running this command, since I have not actually tried it myself:
You could do something like:
for file in <DIR>/*; do sed -i "s/SS-S01/${file##*/}/" "$file"; done
This will loop over each file in <DIR> and for each loop iteration assign the file name to $file. For each file, sed will replace the first occurence of SS-S01 in that file by the file name.

Terminals - Creating Multiple Identical Folders within Subdirectories and Moving Files

I have a bunch of files I'm trying to organize quickly, and I had two questions about how to do that. I really appreciate any help! I tried searching but couldn't find anything on these specific commands for OSX.
First, I have about 100 folders in a directory - I'd like to place an folder in each one of those folders.
For example, I have
Cars/Mercedes/<br>
Cars/BMW/<br>
Cars/Audi/<br>
Cars/Jeep/<br>
Cars/Tesla/
Is there a way I can create a folder inside each of those named "Pricing" in one command, i.e. ->
Cars/Mercedes/Pricing <br>
Cars/BMW/Pricing<br>
Cars/Audi/Pricing<br>
Cars/Jeep/Pricing<br>
Cars/Tesla/Pricing
My second question is a little tougher to explain. In each of these folders, I'd like move certain files into these newly created folders (above) in the subdirectory.
Each file has a slightly different filename but contains the same string of letters - for example, in each of the above folders, I might have
Cars/Mercedes/payment123.html
Cars/BMW/payment432.html
Cars/Audi/payment999.html
Cars/Jeep/payment283.html
Is there a way to search each subdirectory for a file containing the string "payment" and move that file into a subfolder in that subdirecotry - i.e. into the hypothetical "Pricing" folders we just created above with one command for all the subdirectories in Cars?
Thanks so much~! help with either of these would be invaluable.
I will assume you are using bash, since it is the default shell in OS X. One way to do this uses a for loop over each directory to create the subdirectory and move the file. Wildcards are used to find all of the directories and the file.
for DIR in Cars/*/ ; do
mkdir "${DIR}Pricing"
mv "${DIR}payment*.html" "${DIR}Pricing/"
done
The first line finds every directory in Cars, and then runs the loop once for each, replacing ${DIR} with the current directory. The second line creates the subdirectory using the substitution. Note the double quotes, which are necessary only if the path could contain spaces. The third line moves any file in the directory whose name starts with "payment" and ends with ".html" to the subdirectory. If you have multiple files which match this, they will all be moved. The fourth line simply marks the end of the loop.
If you are typing this directly into the command line, you can combine it into a single line:
for DIR in Cars/*/ ; do mkdir "${DIR}Pricing"; mv "${DIR}payment*.html" "${DIR}Pricing/"; done

Change several windows folder names at once

I have to rename several folders. The old folders were randomly named, and the new names allow for a consistent naming protocol. i have reconciled the existing folders with the list of new names, but there are hundreds of folders to rename and to do it manually will take forever.
the old name and new name are generally very different. for example:
old: john l,smth new: smith_john_04082013
so what i would like to do is place a list of the old names in one part of a program and the list of new names in another part, and then loop down the list of folders renaming them until the last one is done.
for example
john l,smth (to) smith_john_04082013
mary-jones 42nd street (to) jones_mary_03122013
wil-h-davis (to) davis_william_02122012
etc
i know how to use the rename command in dos, but all that 'seems' to do is just change the name of of one directory, ie rename "john l,smth" "smith_john_04082013"
i tried doing something like:
rename "john l,smth" "smith_john_04082013"; "mary-jones 42nd street" "jones_mary_03122013"; "wil-h-davis" "davis_william_02122012"
the concept being maybe using a separator was the trick to a multiple rename, but that didn't work either.
if anyone knows how to do this that would be very helpful.
TIA
Just create a batch file (plain text file with a .bat extension, like rename_folders.bat), and list each operation as a single line:
ren "john l,smth" "smith_john_04082013"
ren "mary-jones 42nd street" "jones_mary_03122013"
ren "wil-h-davis" "davis_william_02122012"
(ren is the short name for rename, which saves a little typing.)
Once you have all the lines in the file, save it in the folder where you want it to run, open a command prompt in that folder, and just run the batch file (the .bat extension is optional when you run it, as it's one of the known executable file extensions):
C:\YourBaseFolder>rename_folders
or
C:\YourBaseFolder>rename_folders.bat

Resources