Replace all place holder of all the files in a folder [duplicate] - bash

This question already has answers here:
Renaming part of a filename [duplicate]
(6 answers)
Closed 1 year ago.
I need to write a bash script.
I have a folder name abc and the folder contains multiple (around 20) files. Now some of the files are named as __servicename__List.java Here __servicename is acting as a placeholder. I need to replace this placeholder with a string say xyz
Can someone please help?
Appreciate all your help! Thanks in advance!

In bash I would do:
cd abc
for FileName in **/* ; do
mv -- "$FileName" "${FileName/__servicename__/xyz}"
done

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

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 - how to construct a path in an .sh script using external property? [duplicate]

This question already has answers here:
Reading java .properties file from bash
(13 answers)
Closed 4 years ago.
I have a property s3Path=s3a://myBucket in a file s3-apps.properties
Currently the path to a S3 is hardcoded in my bash script:
thePath="s3a://myBucket/myApp"
I want to get the property from the file instead and concatenate it with /myApp
So I'm getting path to the file like this:
file="/apps/properties/various/s3-apps.properties"
But how do I get the property and concatenate it to construct a path?
I guess that it should be something like this but it did not work for me:
thePath="s3a://{$file.s3Path}/myApp"
Non of the answers from the "Duplicate" helped me in understanding an solving my question. But the #cody answer below was correct and helped.
If you're using GNU grep, the following would work:
$ s3path=$(grep -Po '(?<=s3Path=).+$' "$file")
$ echo $s3path
s3a://myBucket

Moving files to end of path [duplicate]

This question already has answers here:
Moving files to a directory
(6 answers)
Closed 4 years ago.
Would anyone know how to move a file to the very end of a file path? I have tried looking this up but haven’t gotten anywhere. What I’m trying to do is:
mv filename.txt dir1/dir2/dir3/dir4/
I don’t want to list out every directory. Any way to just send it all the way to the end? Any advice or help would be appreciated.
If I understood you, you just need a shell variable to save you from having to repeatedly type out a long path, thus:
p=dir1/dir2/dir3/dir4/
mv filename1.txt $p
mv filename2.txt $p
...
And if you need the $p variable to be always set, you can define it in your .bash_profile or .bashrc file.

I want to move all files from one folder to other folder using shell script [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I want to move all files from one folder to other folder using shell script.
This is my code but it throws error
#!/bin/sh
SRC = '/home/xxx/test1/'
DESTN = '/home/xxx/test/'
mv SRC DESTN
Error:./move.sh:2:./move.sh:SRC:not found
./move.sh:2:./move.sh:SRC:not found
mv:cannot stat 'SRC': No such file or directory
When declaring shell variables, you cannot add spaces between the variable name and the = sign, nor between the = and the value.
Also remember to add $ before the variable name when using it after its declaration.
Your script should look like this one:
#!/bin/sh
SRC="/home/xxx/test1/*"
DESTN="/home/xxx/test"
mv "$SRC" "$DESTN"

Resources