Batch rename files in macOS [duplicate] - bash

This question already has answers here:
Rename files using regular expression in linux
(10 answers)
How to Batch Rename Files in a macOS Terminal?
(8 answers)
Batch rename files regular expression on Mac
(4 answers)
Closed 2 years ago.
I have a lot of files that need renaming.
All of them are named in the following format:
MM DD YYYY filename.txt
These dates are not related to any timestamp in each file. They were given manually when processing them.
I need to change their names to:
YYYY MM DD filename.txt
Do you have any ideas?
I've tried bash and awk scripts, but don't have much experience and all them have failed.
Thanks in advance for anyone who comments here!
Hope everybody is safe and washing their hands. 🙂

Related

How can I bulk rename files ending in invalid characters? [duplicate]

This question already has answers here:
Recursively change file extensions in Bash
(6 answers)
Closed 29 days ago.
I'm on linux Mint. I'd like to recursively rename files like
5f0c74603cbdca44fd877_source.mp4?Tag=1&Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6XC9cL2NkbjIub25seWZhbnMuY29tXC9maWxlc1wvOVwvOWNcLzljNTQzZGI1OGI0ZmQyYWI0YmExMzEzMTUxYmExZjdiXC81ZjBjNzQ2MDNjYmRjYTQ0ZmQ4Nzdfc291cmNlLm1wND9UYWc9MS
1904x2600_8c25949033674d6559bcfd3f02aed68d.jpg?Tag=1&Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6XC9cL2NkbjIub25seWZhbnMuY29tXC9maWxlc1wvYVwvYTFcL2ExNjZmMzRlZGZjMWU3NDRkOGIxZTEwYmZkNDIxNzJjXC8xOTA0eDI2MDBfOGMyNTk0OTAzMzY3NGQ2NTU5
to
5f0c74603cbdca44fd877_source.mp4
1904x2600_8c25949033674d6559bcfd3f02aed68d.jpg
so that I can copy files to an external hard drive.
Anyone know a quick one liner in bash that could accomplish this?
There's also another type of file that cannot be moved
index.html?C=D;O=D
How can I rename that to something that has valid characters?
Thanks!
I see you search to remove all characters after first ? (included).
for f in *\?*; do mv "$f" "${f%%\?*}"; done

How to use whitespace in directories with a Bash script? [duplicate]

This question already has answers here:
How do I pass on script arguments that contain quotes/spaces?
(2 answers)
Bash script to cd to directory with spaces in pathname
(14 answers)
Closed 24 days ago.
I have a script that appends the date and time to all files in a folder. I use the script like this...
bash append_date.sh /home/user/Documents/Podcasts/
and that will append the date to all files in the /home/user/Documents/Podcasts/ folder
Problem is that if there is a whitespace in the directory tree it fails to do anything. ie
bash append_date.sh /home/user/Documents/My Stuff/
I have tried passing the following, but that does not work
bash append_date.sh /home/user/Documents/My\ Stuff/
How do I get this script to play nice with whitespaces?
Many thanks for any help.

Output of date format using Bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
I Have a bash .sh script and I am trying to zip up yesterday's log and archive in a AWS Bucket. I need some help with getting the format of the file name correct but not having luck with it. Here is the script:
#!/bin/bash
/jobs/copytS3.sh /var/log/appname_log_"date" --date="yesterday" +'%Y%m%d'.log
Output shows following:
zip warning: name not matched: /var/log/appname_log_date
Not sure what I am missing. The name of the file should be: appname_log_20210120.log.
I am sure it's something very basic, but I'm unable to resolve. Appreciate any help with the syntax. Thanks!
Try this:
/jobs/copytS3.sh /var/log/appname_log_$(date --date=yesterday +'%Y%m%d').log

BASH Script to check a folder for a specific file type and then delete the oldest one [duplicate]

This question already has answers here:
Bash script to find and display oldest file
(4 answers)
Closed 2 years ago.
I'm looking for a BASH command (or set of commands) that will look in a specific directory and delete ONLY the single oldest file in that directory. I've looked around, but I can't quite find what I'm looking for. Hopefully someone can help me with this, because it's the last missing piece in my script. Everything else is working perfectly.
One way to delete oldest file ending with .specific :
rm -i $(ls -tr *.specific | sed q)
This is not very reliable if you have spaces in filenames

bash string split and concatenate [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 5 years ago.
I have hundreds files need to loop through for an analysis using a bash script. One step I need to do is to split a long string and cat it as an output name. For example, suppose I have one string such like:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final.bam
What I wanted is to rename it as two output file names such as:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R1.fastq
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R2.fastq
The only changes are removing .bam from the original and cat _R1.fastq and _R2_fastq. Does somebody know how to realize it using bash commands?
somefile=blahblahblah.final.bam
foo "$somefile" "${somefile%.*}_R1.fastq" "${somefile%.*}_R2.fastq"

Resources