How to rename multiple files in a directory leaving the extension in Linux [duplicate] - linux

This question already has answers here:
How to rename files without changing extension in Linux 102221.pdf to 102221_name.pdf
(3 answers)
Rename multiple files based on pattern in Unix
(24 answers)
Closed 4 years ago.
I need to rename files in a directory taking out a string of characters that is different with each file but starts the same way. I know how to strip characters from the filename, but how do I preserve the extension? I know it's a variation of a common question but I can't find a answer that fits my exact need.
Redshirts_ep6_dSBHpCsvQ3BfQ7-NNIjXYO4pnHpNMvu7bfvURLF3BSzB_3YOOrBBoNnICTR-hg.mp3
-> Redshirts.mp3
PathsNotTaken_ep6_XWixFER4PJyeozVfcxT96UajpnVI7cRMRhAU4Aj9-rpeacnBleuGY9zCPDe0aQ.mp3
-> PathsNotTaken.mp3

The linux command rename is super helpful here. It can use regex to perform the renaming.
This can probably rewritten a bit, but it appears do to the job here:
rename -n 's/(^[^_]*)_.*/$1.mp3/' *.mp3
Just remove that -n flag to run for for real. Leaving it on is just a test.
This regex says:
Characters at the start of the line ^ that don't contain an underscore [^_] repeated any number of times * are captured into a capture group (^[^_]*) if they are followed by an underscore and any number of any other characters _.*. These are then rewritten by using that first capture group $1 followed by .mp3

Related

Removing everything after last hyphen in a string in Bash script? [duplicate]

This question already has answers here:
How can I remove all text after a character in bash?
(7 answers)
Closed last month.
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg should result in This-is-a-filename
You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*operator removes all beginning with the last hyphen.
You're looking for a sed within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers

What does "?" mean in Unix/Linux? [duplicate]

This question already has an answer here:
Bash - meaning of a simple question mark (?)
(1 answer)
Closed 23 days ago.
I'm currently learning Bash and trying to understand some lines of code which I've found.
So from my understanding,
* refers to everything within the specified file/directory
? refers to one single character
and based off this understanding, I found the code down below and I'm trying to understand it but to no avail.
Can someone please explain what is happening in this line of code?
mv public_html/*.??g public_html/images/
mv public_html/*.??g public_html/images
Move all files in the directory public_html that have a 3 character extension ending in g to its subfolder images.
* is not everything in the directory unless it is all by itself.
public_html/*.??g --> public_html/ anything . single character single character g
Technically, * isn't anything either, it's anything that isn't a /
This may match multiple files, which is ok, because the last pathname on the line appears to be a directory.
To describe this in words, move anything in the public_html directory that ends in a three letter file extension ending in g into the images subdirectory.

How to extract a pattern from a file and append to another on linux [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 6 months ago.
I've a txt file that contains a web page source code , i want to extract all links that contains "https://ANYTHING.amazonaws.com" in it to a new file.
The new file will contain:
https://test-ok.amazonaws.com
https://hhhhh.hhhh.amazonaws.com
https://anything.dd.dd.amazonaws.com
the links doesn't have to be in a specific tag or something, they can be anywhere in any tag!
Thanks!
You can use grep to search for a regex pattern with -o flag to print only the matching fragments and then redirect output to a new file.
In your case probably this one should work:
grep -o 'https://.*\.amazonaws\.com' sourcecode.html > newfile
Here you can find regular expression syntax cheatsheet.

Bash shell script postprocessing results of ls [duplicate]

This question already has answers here:
What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
(4 answers)
What does "##" in a shell script mean? [duplicate]
(1 answer)
Closed 7 months ago.
I came across a shell script like the following:
for FILE_PATH in `ls some/directory`
do
export FILE=${FILE_PATH##*/}
done
What exactly is the "##*/" doing? When I echo ${FILE} and ${FILE_PATH}, I don't see any difference. Is this to handle unusually named files?
More generally, how would I go about figuring out this type of question for myself in the future? Google was completely useless.
It's removing everything up to the last / in the value of $FILE. From the Bash Manual:
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted.
You're not seeing any difference in this case because when you list a directory it just outputs the filenames, it doesn't include the directory portion, so there's nothing to remove. You would see the difference if you did:
for FILE in some/directory/*

Usage of '-' after pipe [duplicate]

This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 5 years ago.
I saw this line of code the other day and I didn't know exactly what is this - or when to use it. This is a simple code and from what I understood is that - takes the piped output and treats it as an argument of paste (correct me if I'am wrong)
seq $size | paste - $file
My question is when can we use this and is there another way to do the same thing?
Thanks,
This is documented in the man page for paste which says:
With no FILE, or when FILE is -, read standard input.
That is, paste expects you to give it a filename, but you can instead give it a single - instead, which paste will interpret as it should read data from stdin , your pipe in this case, instead of opening a file and read data from that file.

Resources