Search for text files in a directory and append a (static) line to each of them - linux

I have a directory with many subdirectories and files with suffixes in those subdirectories (e.g FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA, etc).
How can I recursively search for files with a certain suffix, and append a user-defined line of text to those files? I feel like this is a job for grep and sed, but I'm not sure how I would go about doing it. I'm fairly new to scripting, so please bear with me.

You can do it like
find /where/to/search -type f -iname '*.SUFFIX' -exec echo "USER DEFINED STRING" >> \{\} \;
find searches in the suplied path
-type f finds only files
-iname '*.SUFFIX' find the .SUFFIXed names, case ignored

find ./ -name "*suffix" -exec bash -c 'echo "line_to_add" >> $1' -- {} \;
Basically you use find to get a list of the files. Then you use bash to echo append your line to that list.

Related

bash script parameter all file with beginning with a specified string

I am trying to make the code find all directories start with same letters, this is the code so far. I have two directories lit and lite I should be able to see both directories with I search for lit.
for I in $*
do
echo "the directories $(pwd)/"$1" was modified on "$(date -d "$(stat -c '%y' $1)"
'+%d %d %H:%M'$1)
done
The find command can take the type of files your looking for, and also perform a search for a given name.
find . -type d -name "lit*" -exec ls -ld {} \;
Here we set -type d for directories and -name <search>*" for the name of the files to search for.
You can then execute a command for each result using the -exec parameter

Loop through a directory with any level of depth

I want to execute a command on all files present on all levels in the directory. It may have any number of files and sub directories. Even these sub directories may contain any number of files and subdirectories. I want to do this using shell script. As I am new to this field can any one suggest me a way out.
You can use the command "find" with "xargs" after "|"(pipe).
Example: Suppose that I want to remove all files that have ".txt" extension on "Documents" directory:
find Documents -iname *.txt |xargs rm -f
Helps?
You can use a recursive command that uses wildcard characters (*) like so:
for dir in ~/dev/myproject/*; do (cd "$dir" && git status); done
If you want to apply commands on the individual files you should use the find command and execute commands on it like so:
find yourdirectory -type f -exec echo "File found: '{}'" \;
What this does:
finds all the items in the directory yourdirectory
that have the type f - so are a file
runs an exec on each file
Use find:
find -type f -exec COMMAND {} \;
-f applies the command only to files, not to directories. The command is recursive by default.

Scan For Multiple File Exentions and Copy To New Dir If Found

I am using Ubuntu and I am needing to recursively search a directory and subs for any .mp4, .mkv, or .wav files. I saw that mmv may be my best solution, but I can not get the syntax correct. So how would I convert this to search for an array of file names, or would I just want to run 3 iterations for each file extension I am after?
I was using the tutorial here to write this, so please forgive me if I am way outta line
find all m3
# find / -iname "*.mp4" -type f -exec mv {} ./Downloads/Media Files \;
With GNU bash 4:
shopt -s globstar nullglob
mv -v **/*.{mp4,mkv,wav} ./Downloads/Media Files
globstar: If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
nullglob: If set, bash allows patterns which match no files (see Pathname Expansion) to expand to a null string, rather than themselves.
You can use \( and -o (or):
find / -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.wav" \) -exec mv {} ./Downloads/Media Files \;
Replace -iname with -regex. Regex understands emacs regular expressions by default (but you can change this behaviour using -regextype):
find / -regex ".*\.mp4\|.*\.mkv\|.*\.wav" ...
Learn the power of reguar expressions, it will open a new universe of power!

Find and replace string in a tree. Recursive

I have tried searching for answer here and I found some but they dont seem to work for me. I want to rename a project file and I find it that the esiest way would be to replace its name in every single file and ALSO every file name as well in the directory.
Could someone please point me a way (in windows or linux) to replace recursively a string in a directory in every file and every file name, with another string?
I would appreciate all help.
Bash
In Unix you can do it in bash shell. You have to make use of find, mv and sed and the following three commands as examples:
Replace string "ABC" with "345" in the name of every directory
in the current directory or in the subdirectories recursively.
find . -depth -type d -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the name of every file in
the current directory or in the subdirectories recursively.
find . -depth -type f -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the content of every file
in the current directory or in the subdirectories recursively.
find . -depth -type f -name "*" -exec bash -c 'sed -i "s/ABC/345/g" "$1"' par0 {} \;

How to gzip all files in all sub-directories in bash

I want to iterate among sub directories of my current location and gzip each file seperately. For zipping files in a directory, I use
for file in *; do gzip "$file"; done
but this can just work on current directory and not the sub directories of the current directory. How can I rewrite the above statements so that It also zips the files in all subdirectories?
I'd prefer gzip -r ./ which does the same thing but is shorter.
No need for loops or anything more than find and gzip:
find . -type f ! -name '*.gz' -exec gzip "{}" \;
This finds all regular files in and below the current directory whose names don't end with the .gz extension (that is, all files that are not already compressed). It invokes gzip on each file individually.
Edit, based on comment from user unknown:
The curly braces ({}) are replaced with the filename, which is passed directly, as a single word, to the command following -exec as you can see here:
$ touch foo
$ touch "bar baz"
$ touch xyzzy
$ find . -exec echo {} \;
./foo
./bar baz
./xyzzy
find . -type f | while read file; do gzip "$file"; done
I can't comment on the top post (yet...), but I read in the man pages of "find" that -execDir is safer than -exec because the command is done in the subdirectory where the match is found, rather than the parent directory where "find" is ran from.
If anyone would like to use a regex with to locate specific files in a subdirectory to zip, I'd recommend using
find ./ -type f -name 'addRegexHere' -execdir gzip -k "{}" \;
if you don't need regex's, stick with the recursive gzip call above (or below, if I gain any traction haha)
source

Resources