Loop over directories [duplicate] - bash

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

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 cp filenames with spaces [duplicate]

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"

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.

Renaming Multiple Files on macOS Terminal [duplicate]

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*

Resources