modify shell script to delete folders as well as files - bash

My shell script:
#!/bin/bash
if [ $# -lt 2 ]
then
echo "$0 : Not enough argument supplied. 2 Arguments needed."
echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
echo "Followed by some path to remove files from. (path of where to look) "
exit 1
fi
if test $1 == '-d'
then
find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
find $2 -mmin +60 -type f -exec rm -rf {} \;
fi
Basically this will find files in a given directory provided as second argument and either list (-d for argument 1) or remove (-e for argument 1) files modified >60 minutes ago.
How can I rework this to also remove folders ?

Remove -type f
changing ls -l to ls -ld
Change 1 will list everything and not just files. This includes links as well. If you are not fine with listing/deleting anything other than files and directories then you need to separately list/delete files and directories as:
if test $1 == '-d'
then
find $2 -mmin +60 -type f -exec ls -ld {} \;
find $2 -mmin +60 -type d -exec ls -ld {} \;
elif test $1 == '-e'
then
find $2 -mmin +60 -type f -exec rm -rf {} \;
find $2 -mmin +60 -type d -exec rm -rf {} \;
fi
Change 2 is needed as ls -l on a directory will list the files in the directories.

#!/bin/bash
if [ $# -lt 2 ]
then
echo "$0 : Not enough argument supplied. 2 Arguments needed."
echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
echo "Followed by some path to remove files from. (path of where to look) "
exit 1
fi
if test $1 == '-d'
then
find $2 -mmin +60 -type d -exec ls -l {} \;
find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
find $2 -mmin +60 -type d -exec rm -rf {} \;
find $2 -mmin +60 -type f -exec rm -rf {} \;
fi
That should work for you.

Related

Deleting all the subfolders having fewer than X files

I would like to remove all the subfolders having less than X file in a folder
The following code search those subfolders with less than X file:
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -print
./digna_1919
./digna_2040
./digna_1682
(more output omitted)
So I can find them! But if I do this, I get "Directory not empty":
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -delete
find: cannot delete ‘./digna_1919’: Directory not empty
find: cannot delete ‘./digna_2040’: Directory not empty
find: cannot delete ‘./digna_1682’: Directory not empty
(more output omitted)
And if I do this, I get "No such file or directory":
$ find . -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -exec rm -r "{}" \;
find: ‘./digna_1919’: No such file or directory
find: ‘./digna_2040’: No such file or directory
find: ‘./digna_1682’: No such file or directory
(more output omitted)
Where am I doing wrong? Thanks a lot!
Source of the code
Use -depth to have find process each directory's contents before the directory itself. You'll want to do this any time you delete items.
$ find . -depth -type d -exec sh -c 'set -- "$0"/*; [ $# -le 10 ]' {} \; -exec rm -r {} \;

find: How to use found paths in the -exec directive?

I have a dozen files named
~/DOMAIN1.de/bin/dbdeploy.php
~/DOMAIN2.de/bin/dbdeploy.php
~/DOMAIN3.de/bin/dbdeploy.php
I want to run them all with the same arguments.
My bash script reads:
cd ~
find . -maxdepth 1 -type d -name "*\.de" -exec php56 bin/dbdeploy.php "$1" "$2" \;
However, the path given to exec seems not to be relative to the found subdirectory but rather to my PWD:
$ bash -x ./.dbpush "some argument"
+ cd ~
+ find . -maxdepth 1 -type d -name '*\.de' -exec php56 bin/dbdeploy.php 'some argument' ';'
Could not open input file: bin/dbdeploy.php
Could not open input file: bin/dbdeploy.php
Could not open input file: bin/dbdeploy.php
How can I use the found path in the -exec directive?
Ok, actually I found the answer myself:
The "find"-results are stored in {}, so the line reads
find . -maxdepth 1 -type d -name "*\.de" -exec php56 {}/bin/dbdeploy.php "$1" "$2" \;
Alternativly
find . -type f -wholename "*\.de/bin/dbdeploy.php" -exec php56 {} "$1" "$2" \;

Bash - create subfolders for folders recently created

I want to "join" these two tasks:
for dir in /blabla/bleble/*; do (cd "$dir" && mkdir -p Folder1/Folder1a && mkdir -p Folder2); done
and
find -amin -10
How can I do this?
I've tried this, but it doesn't work:
find -amin -2 -exec sh -c '
for dir in /blabla/bleble/*; do (cd "$dir" && mkdir -p Folder1/Folder1a && mkdir -p Folder2);
done' sh {} +
Something like this is might do:
find /a/b/ -mindepth 1 -maxdepth 1 -type d -amin -2 \
-exec sh -c 'for f; do mkdir -p -- "$f/Folder1/Folder1a" "$f/Folder2; done"' "" {} +
Breakdown:
-mindepth 1 -maxdepth 1 will limit result to current directory only.
-type d makes sure you only list directories
-exec foo "" {} + will execute foo with matches as arguments:
foo "" "/a/b/c" "/a/b/john" "a/b/doe"
for f will iterate over all positional arguments ($1, $2, ...)
for f; do
mkdir -p -- "$f/Folder1/Folder1a" "$f/Folder2"
done
Running sh -c 'code' arg1 arg2 will set $0 to arg1, and $1 to arg2, therefore the empty argument: foo "" {} +:
% sh -c 'echo $0' john
john
Assuming there aren't so many folders in /blabla/bleble that you overflow the command line, you can use find to search the target directory. -prune prevents recursing into the directories.
find /blabla/bleble/* -prune -type d -amin -10 -exec mkdir -p {}/Folder1/Folder1a {}/Folder2 \;
If you are using GNU find or another version that supports them, use -mindepth and -maxdepth instead to find the top-level subdirectories, no matter how many there are.
find /blabla/bleble -maxdepth 1 -mindepth 1 -type d -amin -10 -exec mkdir -p {}/Folder1/Folder1a {}/Folder2 \;

ls command and size of files in shell script

count=0; #count for counting
IFS='
'
for x in `ls -l $input`; #for loop using ls command
do
a=$(ls -ls | awk '{print $6}') #print[6] is sizes of file
echo $a
b=`echo $a | awk '{split($0,numbers," "); print numbers[1]}'`
echo $b
if [ $b -eq 0 ] # b is only size of a file
then
count=`expr $count + 1` #if b is zero , the count will increase one by one
fi
echo $count
done
I want to find 0 size files . I do that using find command. The second thing is I want to count number of has 0 size of files using ls command and awk. But It doesn't true code . What is my mistake ?
The -s test is true if a file has non-zero size. If that test fails for file, increment your empty-file count.
empty_files=0
for f in "$input"/*; do
[ -s "$f" ] || : $(( empty_files++ ))
done
Your main mistake is that you're parsing ls!
If you want to find (regular) files that are empty, and if you have a version of find that supports the -empty predicate, use it:
find . -type f -empty
Note that this will recurse in subfolders too; if you don't want that, use:
find . -maxdepth 1 -type f -empty
(assuming that your find also supports -maxdepth).
If you only want to count how many empty (regular) files you have:
find . -maxdepth 1 -type f -empty -printf x | wc -m
and if you want to perform both operations at the same time, i.e., print out the name or save them in an array for future use, and count them:
empty_files=()
while IFS= read -r -d '' f; do
empty_files+=( "$f" )
done < <(find . -maxdepth 1 -type f -empty -print0)
printf 'There are %d empty files:\n' "${#empty_files[#]}"
printf ' %s\n' "${empty_files[#]}"
With Bash≥4.4, you could use mapfile instead of the while-read loop:
mapfile -t -d '' empty_files < <(find . -maxdepth 1 -type f -empty -print0)
printf 'There are %d empty files:\n' "${#empty_files[#]}"
printf ' %s\n' "${empty_files[#]}"
For a POSIX-compliant way, use test with the -s option:
find . -type f \! -exec test -s {} \; -print
and if you don't want to recurse into subdirectories, you'll have to -prune them:
find . \! -name . -prune -type f \! -exec test -s {} \; -print
and if you want to count them:
find . \! -name . -prune -type f \! -exec test -s {} \; -exec printf x | wc -m
and here, if you want to perform both operations (count them and save them in an array for later use), use the previous while-read loop (or mapfile if you live in the future) with this find:
find . \! -name . -prune -type f \! -exec test -s {} \; -exec printf '%s\0' {} \;
Also see chepner's answer for a pure shell solution (needs minor tweaking to be POSIX compliant).
Regarding your comment
I want to count and delete [empty files]. How can I do that at the same time?
If you have GNU find (or a find that supports all the goodies):
find . -maxdepth 1 -type f -empty -printf x -delete | wc -m
if not,
find . \! -name . -prune -type f \! -exec test -s {} \; -printf x -exec rm {} \; | wc -m
Make sure that the -delete (or -exec rm {} \;) predicate is at the end! do not exchange the order of the predicates!

xargs how to put result {} into $(cmd {})?

for example:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|xargs -I{} echo "{} =>" $(realpath {})
I would like it to output:
/usr/lib/libblas.so.3gf=>/usr/lib/libblas/libblas.so.3gf.0
/usr/lib/libblas.so=>/usr/lib/libblas/libblas.so.3gf.0
/usr/lib/libblas.a=>/usr/lib/libblas/libblas.a
This will not work because the value in $() is expanded before the script actual running.
Is there any way I can achieve this result? without loop in bash?
Alternatively:
find /usr/lib -maxdepth 1 -type l \
-exec echo -n '{} =>' \; \
-exec realpath '{}' \;
Have xargs call the shell:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|xargs -I{} sh -c 'echo "{} =>" $(realpath {})'
You need the command substitution to happen after the file name is known. So you need xargs to call a shell and do the substitution there.
Since you're running the command on a single file at a time, using xargs is a useless complication (and it also mangles some file names). Use -exec!
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'echo "$0 => $(realpath "$0")' {} \;
You could make this slightly faster and not less clear by not using a command substitution:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'echo -n "$0 => "; realpath "$0"' {} \;
To make things a little faster, don't invoke a new shell process for every file:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'for x; do echo -n "$x => "; realpath "$x"; done' _ {} +
(You can do the same with xargs, but just drop xargs and stick to the simpler, faster, more robust -exec.)
Try to convert each filename separately using line-by-line "while" loop:
find ... | while read f; do echo "$f" '=>' "$(realpath $f)" ; done
The shortest seems to be using GNU Parallel:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|parallel echo {} '=\> $(readlink -f {})'

Resources