This question already has answers here:
Batch renaming files with Bash
(10 answers)
Closed 5 years ago.
Is it possible to rename multiple files that share a similar name but are different types of files all at once?
Example:
apple.png
apple.pdf
apple.jpg
Can I substitute the apple for something else, for example "pear"? If this is possible, what would the command be? Many thanks for your time!
You can do this in bash natively by looping over the files beginning apple and renaming each one in turn using bash parameter expansion
$ for f in apple*; do mv "$f" "${f/apple/pear}"; done
The for f in apple* finds all files matching the wildcard. Each filename is then assigned to the variable f
For each assignment to f bash calls the command mv to move (rename) the file from it's existing name to one where apple is replaced by pear
You could also install rename using a package manager like Homebrew and call
rename -e 's/apple/pear/' apple*
Related
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
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
This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 4 years ago.
I'm using variables in bash script to hold folder names (to iterate over multiple folders).
I'd like to copy files from one to another, the files exist in source directory. Folders and filenames contain spaces, so I must use double quote.
For instance:
#!/bin/bash
inpath="~/foo bar/"
outpath="~/temp basket/
cp "$inpath*" "$outpath"
The copy fails as: '~/foo bar/*' No such file or directory
Is there any consistent way to do that?
Only quote the parts you don't want expanded or split:
inpath=~/'foo bar'
outpath=~/'temp basket'
cp -- "$inpath/"* "$outpath"
This question already has answers here:
How to loop over directories in Linux?
(11 answers)
Closed 7 years ago.
I'm not sure how to handle directories of directory scenario in shell.
I have folder structure as below.
Directory structure:
/DirA/DirA1/DirA11/*.txt
/DirA2/DirA21/*.txt
/DirA3/DIrA31/*.txt'
I'm new to shell scripting, not able to figure out how to read these text files.
You can use the find command to process all files with certain properties in a directory tree. For example,
find /DirA* -name '*.txt' 2>/dev/null
would list all files named *.txt inside the trees you are mentioning. Note that if you use wildcards in the name mask, you need to single-quote them in order to protect them from the shell.
for f in /DirA/DirA1/DirA11/*.txt /DirA2/DirA21/*.txt /DirA3/DIrA31/*.txt; do
# do stuff with $f
done
This question already has answers here:
Get most recent file in a directory on Linux
(23 answers)
Closed 4 years ago.
I need to get the latest directory name in a folder which start with nlb.
#!/bin/sh
cd /home/ashot/checkout
dirname=`ls -t nlb* | head -1`
echo $dirname
When the folder contains many folders with name starting nlb, this script works fine, but
when there is only one folder with name starting nlb, this script prints the latest file name inside that folder. How to change it to get the latest directory name?
Add the -d argument to ls. That way it will always print just what it's told, not look inside directories.
#!/bin/sh
cd /home/ashot/checkout
dirname=$(ls -dt nlb*/ | head -1)
echo $dirname
As the other answer points it out, you need the -d to not look inside directories.
An additional tip here is appending a / to the pattern. In the question you specified to get the latest directory. With this trailing / only directories will be matched, otherwise if a file exists that is the latest and matches the pattern nlb* that would break your script.
I also changed the `...` to $(...) which is the modern recommended writing style.