Given an array with filenames, how to find and delete all matching files in bash? [closed] - linux

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Given a blacklist.txt file with filenames:
.picasa.ini
Thumbs.db
._.DS_store
How can I best find files with those filenames and delete them? I tried:
readarray -t blacklisted < ./Blacklist.txt
for n in ${blacklisted[#]};do find . -type f -name "${n}" -delete; done
But it doesn't work for me.

Read the file line by line, and launch the rm command on each iteration.
#!/bin/bash
filename='blacklist.txt'
echo Start
while read p; do
echo "removing $p ..."
find . -name "$p" -exec rm {} \;
done < "$filename"
Add the -f flag to the rm command if you feel confident.

Related

Bash Script: Check if multiple files are executable and output them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
Can you make a script that iterates through a folder (for example the home directory) and prints the names of all regular files that are executable?
Are you aware that the find command has an -executable switch?
If you want to see all executable files in all subdirectories, you might do:
find ./ -type f -executable
If you want to see all executable files, but just in your directory, you might do:
find ./ -maxdepth 1 -type f -executable
I can.
#!/bin/bash
for d in "$#"
do [[ -d "$d" ]] || { printf '\n"%s" not a directory\n' "$d"; continue; }
for f in "$d"/* "$d"/.*; do [[ -f "$f" && -x "$f" ]] && ls -l "$f"; done
done
But use find as Dominique advised.
Why reinvent the wheel?
Still, there's a lot going on there that could be useful.
Let me know if you have questions.

How to gzip a file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
How can I gzip this file? I get an error:
find /users/tnea01/logfile10.log -type f -exec sh -c \ 'gunzip /users/tnea01/logfile_archive/$(basename $0)_$(date -r {} +%F).gz $0' {} \;
Here is the error I get:
gzip: /users/tnea01/logfile10.tar.gz: No such file or directory gzip: /users/tnea01/logfile10.log: unknown suffix -- ignored
If you didn't know the exact filename, you might do something like this:
find /users/tnea01 -maxdepth 1 -name '*.log' -type f -exec sh -c \
'for f; do
gzip -c <"$f" >"/users/tnea01/logfile_archive/${f##*/}_$(date -r "$f" +%F).gz"
done' _ {} +
To explain the moving pieces:
The only secure way to use sh -c is with a completely constant string; substituting variables into it (including filenames) creates security vulnerabilities. Thus, we don't use any kind of replacement facility in the code, but pass the filename(s) as extra arguments.
for f; do is the same as for f in "$#"; do -- it iterates over all command line arguments.
${f**#/} evaluates to everything after the last / in $f; see the bash-hackers page on parameter expansion.
Expansions, including $(date ...), need to be inside a double-quoted context to be safe; here, we're putting the entire destination filename in such quotes.
However, since you do, that's all entirely needless.
f=/users/tnea01/logfile10.log
d=/users/tnea01/logfile_archive
gzip -c <"$f" >"$d/${f##*/}_$(date -r "$f" +%F).gz"

find a pattern in files and rename them [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I use this command to find files with a given pattern and then rename them to something else
find . -name '*-GHBAG-*' -exec bash -c 'echo mv $0 ${0/GHBAG/stream-agg}' {} \;
As I run this command, I see some outputs like this
mv ./report-GHBAG-1B ./report-stream-agg-1B
mv ./reoprt-GHBAG-0.5B ./report-stream-agg-0.5B
However at the end, when I run ls, I see the old file names.
You are echo'ing your 'mv' command, not actually executing it. Change to:
find . -name '*-GHBAG-*' -exec bash -c 'mv $0 ${0/GHBAG/stream-agg}' {} \;
I would suggest using the rename command to perform this task. rename renames the filenames supplied according to the rule specified as a Perl regular expression.
In this case, you could use:
rename 's/GHBAG/stream-agg/' *-GHBAG-*
In reply to anumi's comment, you could in effect search recursively down directories by matching '**':
rename 's/GHBAG/stream-agg/' **/*-GHBAG-*
This works for my needs, replacing all matching files or file types. Be warned, this is a very greedy search
# bashrc
function file_replace() {
for file in $(find . -type f -name "$1*"); do
mv $file $(echo "$file" | sed "s/$1/$2/");
done
}
I will usually run with find . -type f -name "MYSTRING*" in advance to check the matches out before replacing.
For example:
file_replace "Slider.js" "RangeSlider.ts"
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.ts
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.ts
or ditch the filetype to make it even greedier
file_replace Slider RangeSlider
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.js
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.js
renamed: stories/theme/Slider.css -> stories/theme/RangeSlider.css

exclude directories mv unix [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
The command below moves every hidden/normal file ending with *string without . or _ before it.
mv {.,}*[!._]string /destination
How can I also exclude moving all directories in the above command?
Try
find /WHERE/TO/FIND -name '*STRING' \( ! -name '*_STRING' -o ! -name '*.STRING' \) -type f -exec mv \{\} /WHERE/TO/MOVE \;
Note, if you want to move every file from only the /WHERE/TO/FIND directory, you should add -maxdepth 1 (after e.g. the -type f part).
How about:
for file in {.,}*[!._]string; do test -f "$file" && mv "$file" /destination; done
In what shell does the [!._] glob actually work when used with {.,}? You would probably be better off avoiding the {} notation and do:
for file in .*[!._]string *[!._]string; do ... ; done

Change filenames to lowercase in Ubuntu in all subdirectories [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I know it's been asked but what I've found has not worked out so far.
The closet I came is this : rename -n 'y[A-Z]/[a-z]/' *
which works for the current directory. I'm not too good at Linux terminal so what
should I add to this command to apply it to all of the files in all the sub-directories from which I am in, thanks!
Here's one way using find and tr:
for i in $(find . -type f -name "*[A-Z]*"); do mv "$i" "$(echo $i | tr A-Z a-z)"; done
Edit; added: -name "*[A-Z]*"
This ensures that only files with capital letters are found. For example, if files with only lowercase letters are found and moved to the same file, mv will display the are the same file error.
Perl has a locale-aware lc() function which might work better:
find . -type f | perl -n -e 'chomp; system("mv", $_, lc($_))'
Note that this script handles whitespace in filenames, but not newlines. And there's no protection against collisions, if you have "ASDF.txt" and "asdf.txt" one is going to get clobbered.

Resources