Rename a portion of a filename recursively throughout directory and sub directory [duplicate] - linux

This question already has answers here:
Rename files and directories recursively under ubuntu /bash
(8 answers)
Closed 1 year ago.
How can I make this command also execute throughout sub directories?
for filename in *foo*; do mv "$filename" "${filename//foo/bar}"; done

Probably you want to rename only the filename (last pathname component), not a inbetween subdirectory name. Then this task can be accomplished using globstar feature of bash.
#!/bin/bash
shopt -s globstar
for pathname in ./**/*foo*; do
[[ -f $pathname ]] || continue
basename=${pathname##*/}
mv "$pathname" "${pathname%/*}/${basename//foo/bar}"
done

Related

Shell script to list all files in a directory [duplicate]

This question already has answers here:
How to get the list of files in a directory in a shell script?
(11 answers)
Closed 12 months ago.
I am using the following code :
#!/bin/bash
for f in $1 ; do
echo $f
done
The aim is to list down all the files in the directory that is passed as an argument to this script. But it's not printing anything. Not sure what could be wrong with this.
Try this Shellcheck-clean pure Bash code for the "further plan" mentioned in a comment:
#! /bin/bash -p
# List all subdirectories of the directory given in the first positional
# parameter. Include subdirectories whose names begin with dot. Exclude
# symlinks to directories.
shopt -s dotglob
shopt -s nullglob
for d in "$1"/*/; do
dir=${d%/} # Remove trailing slash
[[ -L $dir ]] && continue # Skip symlinks
printf '%s\n' "$dir"
done
shopt -s dotglob causes shell glob patterns to match names that begin with a dot (.). (find does this by default.)
shopt -s nullglob causes shell glob patterns to expand to nothing when nothing matches, so looping over glob patterns is safe.
The trailing slash on the glob pattern ("$1"/*/) causes only directories (including symlinks to directories) to be matched. It's removed (dir=${d%/}) partly for cleanliness but mostly to enable the test for a symlink ([[ -L $dir ]]) to work.
See the accepted, and excellent, answer to Why is printf better than echo? for an explanation of why I used printf instead of echo to print the subdirectory paths.
If you only need to list files not directories. (this part is unclear to me.) find is your friend.
find $1 -depth 1 -type file
Returns:
./output.tf
./locals.tf
./main.tf
./.tflint.hcl
./versions.tf
./.pre-commit-config.yaml
./makefile
./.terraformignore
./jenkins.tf
./devops.tf
./README.md
./.gitignore
./variables.tf
./Jenkinsfile
./accounts.tf
./.terraform.lock.hcl
Furthermore, please run man find.

Writing a bash script to find all files in a directory that start with a, and do nothing if one exist [duplicate]

This question already has answers here:
Do not show results if directory is empty using Bash
(3 answers)
Closed 4 years ago.
So I have to find all the files in the directory that start with the letter a, and list them out. This is pretty easy by doing
cd some_directory
for file in a*; do
echo "$file"
done
However I want that if there are no files present that match a*, then the for loop will not run at all. Currently, if this is the case then the shell will echo
a*
Is there a way to do this? Thank you
Your text is opposite of your title, in my answer below I've assumed the text is your intention and your title is incorrect:
globs can be made to act like this with the bash shell option "nullglob":
shopt -s nullglob
An alternative is to use find and ignore errors by piping stderr to /dev/null
for file in $(find a* 2>/dev/null); do
echo "$file"
done

For loop in bash [duplicate]

This question already has answers here:
How to loop through a directory recursively to delete files with certain extensions
(16 answers)
BASH: Writing a Script to Recursively Travel a Directory of N Levels
(2 answers)
Closed 4 years ago.
I'm trying to write a bash script that recursively goes through files in a directory, writing the file's name and hexdump to a file. My current script:
#/bin/sh
touch hexdump.txt
for filename in logical/*; do
echo "$filename"
"$filename" >> hexdump.txt
hd /logical/"$filename" >> hexdump.txt
done
The current output is:
logical/*
./hexadecimalExtraction.sh: line 5: logical/*: No such file or directory
hd: /logical/logical/*: No such file or directory
How do i get it to interpret "logical/*" as the list of files within "logical" directory and not the filename itself???
"$filename" >> hexdump.txt
should probably be removed
Otherwise you are trying to run the filename itself.
Also you are looking for files in logical subdirectory in the current directory, but the trying to look in /logical/
You can't recurse with for filename in logical/*. In order to recurse, you have to use find.
To make find visit only files, not directories, use find -type f.
I don't know hd, but you probably want
find tutorials -type f | while read i; do
echo $i >> hexdump.txt
hd $i >> hexdump.txt
done
You're looking for the ** glob operator.
shopt -s globstar nullglob
for filename in logical/**/*; do
echo "$filename"
hd "$filename"
done >> hexdump.txt
filename will contain the full name of the matched files, which already includes the directory logical and any sub directories.

star wildcard in bash [duplicate]

This question already has answers here:
Rename multiple files in shell [duplicate]
(4 answers)
Closed 4 years ago.
I've got a small problem with my bash script. I try to change file name in current directory for whole files with txt extension to text extension. For exampel 1.txt to 1.text
My script looks like this now:
#!/bin/bash
FILES=`ls /home/name/*.txt`
NAME=*.txt
RENAME=*.text
for file in FILES
do
mv $NAME $RENAME
done
i try whole combination with single, double quotes and backticks and I receive errors all the time.
Do you have some ideas how to receive wildcards "*" in bash?
Thanks.
That's not at all how you do that.
#!/bin/bash
shopt -s nullglob
OLD=.txt
NEW=.text
FILES=(/home/name/*"$OLD")
for file in "${FILES[#]}"
do
mv "$file" "${file%$OLD}$NEW}"
done
There are a number of issues with your script. Firstly, you shouldn't run ls and attempt to store its output like that. If you want to iterate through those file, just do it in the loop:
for file in /home/name/*.txt
Now the shell is doing all the work for you, and as a bonus handling any kind of weird filenames that you might have.
In your example you were looping over the literal string "FILES", not the variable, but I guess that was just a typo.
The built-in way to change the filename is to use a parameter expansion to remove the old one, then concatenate with the new one:
old=txt
new=text
for file in /home/name/*"$old"
do
mv "$file" "${file%$old}$new"
done
If it is possible that there are no files matching the glob, then by default, the /home/name/*.txt will not be expanded and your loop will just run once. then you have a couple of options:
use shopt -s nullglob so that /home/name/*.txt expands to null, and the loop is skipped
add an explicit check inside the loop to ensure that $file exists before trying to mv:
for file in /home/name/*"$old"
do
[ -e "$file" ] || continue
mv "$file" "${file%$old}$new"
done
You can use rename to rename filenames.
rename .txt .text /home/name/*.txt
And if you want to do this by looping, you can
for FILE in /data/tmp/*.txt; do
mv "${FILE}" "${FILE/.txt/.text}"
done

how to delete filename's specific suffix in a dir, in linux [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Extract filename and extension in bash
Linux: remove file extensions for multiple files
For example, A.txt B.txt, I want to rename then to A and B .
How can I do it with shell script? Or other method ? Thanks.
for i in *.txt; do mv "$i" "${i%.txt}"; done
I would use something like:
#!/bin/bash
for file in *.txt
do
echo "$file" "$( echo $file | sed -e 's/\.txt//' )"
done
Of course replace the two above references of ".txt" to whatever file extension you are removing, or, preferably just use $1 (the first passed argument to the script).
Michael G.
for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done

Resources