Deleting a directory starting with a specific string in shell script - linux

When I'm trying to delete all directories starting with tmp,
I used this command:
find -type d -name tmp* -exec rmdir {} \;
And it does the trick, but this command is exiting with an error code:
find: `./tmp09098': No such file or directory
What causing failing my build.
Can anyone tell me how I can delete those folders without getting the error?
After trying what #anubhava suggested and quoted 'temp*',
find -type d -name 'tmp*' -exec rmdir {} \;
I still get the same error:
find: `./tmp0909565g': No such file or directory
find: `./tmp09095': No such file or directory
When running:
find -type d -name 'tmp*' -exec ls -ld '{}' \;
This is the result:
drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp0909565g
drwxr-xr-x 2 root root 4096 Jun 16 10:07 ./tmp09095
drwxr-xr-x 2 root root 4096 Jun 16 10:08 ./tmp09094544656

You should quote the pattern otherwise it will be expanded by shell on command line:
find . -type d -name 'tmp*' -mindepth 1 -exec rm -rf '{}' \; -prune
-prune causes find to not descend into the current file/dir.

This works for me:
#! /bin/bash
tmpdirs=`find . -type d -name "tmp*"`
echo "$tmpdirs" |
while read dir;
do
echo "Removing directory $dir"
rm -r $dir;
done;

Related

printing directory with simple ls and grep command Linux

So I have this command ls -al -R | grep libbpf.h and it just act dump print
-rw-r--r-- 1 root root 53107 جنوری 27 12:05 libbpf.h
I also need the exact subdirectories that contain this file is there a way I can use the above command with some option for grep or ls so it also prints some thining like
-rw-r--r-- 1 root root ./libbpf/src/include/libbpf.h 53107 جنوری 27 12:05 libbpf.h
so I only knows the the libbpf.h does exists in somewhere from root directory recursively searching just give me the path, does any one knows this
you can use find command
find "$(pwd -P)" -type f -name "libbpf.h" -ls
if you want only paths
find "$(pwd -P)" -type f -name "libbpf.h"
or
find . -type f -name "libbpf.h" -exec realpath {} \;

Insert pwd on exec command

When i run this commannd:
find . -name "*.sh" -exec ls -ltrh {} ;, i have this output:
-rw-r--r-- 1 root root 7.0K Jun 24 19:37 ./ubuntu.sh
-rw-r--r-- 1 root root 6.8K Jun 24 19:35 ./centos.sh
I want to insert the pwd command to see where is location of ubuntu and centos .sh.
I want to know how to solve tthis problem
find \`pwd\` -name "*.sh" -exec ls -ltrh {} \\;
Should do it. That's backquotes around pwd.

Delete all directories excluding a specific one

On a linux host, given an absolute path, I want to delete all except a certain directory.
To simplify things below is the directory structure and I want to delete all directories except test2
[root#hostname test]# pwd
/opt/data/test
root#hostname test]# ls -ltr
total 8
drwxr-xr-x 5 root root 4096 Dec 5 09:33 test1
drwxr-xr-x 2 root root 4096 Dec 5 09:44 test2
[root#hostname test]#
I looked into How to exclude a directory in find . command and tried the prune switch like this
[root#hostname test]# find /opt/data/test -type d -path test2 -prune
-o ! -name "test2" -print
/opt/data/test
/opt/data/test/test1
/opt/data/test/test1/ls.txt
/opt/data/test/test1/test13
/opt/data/test/test1/test13/temp.py
/opt/data/test/test1/test13/try.pl
/opt/data/test/test1/test11
/opt/data/test/test1/test11/ls.txt
/opt/data/test/test1/test11/temp.py
/opt/data/test/test1/test11/try.pl
/opt/data/test/test1/test12
/opt/data/test/test1/test12/ls.txt
/opt/data/test/test1/test12/temp.py
/opt/data/test/test1/test12/try.pl
/opt/data/test/test1/temp.py
/opt/data/test/test1/try.pl
/opt/data/test/test2/ls.txt
/opt/data/test/test2/temp.py
/opt/data/test/test2/try.pl
[root#hostname test]
now it lists all the folder including /opt/data/test and if I add the xargs rm -rf to this, it will delete the parent folder as well. I don't think I understood the concept -path and -name correctly, please help
Using a simple negation with -not may be easier than pruning:
$ find /opt/data/test -type d -not -name test2
EDIT:
There's no reason to recurse in to the subdirectories, since you're going to delete the top directories anyway, so you could add -maxdepth and avoid finding the directories inside test2:
$ find /opt/data/test -maxdepth 1 -type d -not -name test2
I was able to achieve the required behavior by adding -mindepth 1 to the find command to exclude the parent directory.

crontab is not deleting the files in linux

I am trying to delete all the pdf files which are more than 30 days old at 11:30 PM
I added the below given code in crontab
30 23 * * * find /var/www/html/site/reports/ -name "*.pdf" -type f -mtime +30 | xargs -I {} rm -f {} \;
But it doesn't delete the files.
Can you please check what the issue is?
The crontab details
-rw-r--r--. 1 root root 532 Sep 30 11:14 crontab
One of the files which i need to delete
-rw-r--r-- 1 apache apache 15215 Jul 25 11:24 sales_report.pdf
You missed user and PATH. This may help
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
30 23 * * * root find /var/www/html/site/reports/ \( -name "*.pdf" \) -type f -mtime +30 -exec rm {} \; >> /tmp/debug_cron 2>&1
And then check /tmp/debug_cron
I have this working a a linux box.
30 23 * * * find /var/www/html/site/reports/ \( -name "*.pdf" \) -type f -mtime +30 -exec rm {} \;

Find and rename a directory

I am trying to find and rename a directory on a linux system.
the folder name is something like : thefoldername-23423-431321
thefoldername is consistent but the numbers change every time.
I tried this:
find . -type d -name 'thefoldername*' -exec mv {} newfoldername \;
The command actually works and rename that directory. But I got an error on terminal saying that there is no such file or directory.
How can I fix it?
It's a harmless error which you can get rid of with the -depth option.
find . -depth -type d -name 'thefoldername*' -exec mv {} newfoldername \;
Find's normal behavior is to process directories and then recurse into them. Since you've renamed it find complains when it tries to recurse. The -depth option tells find to recurse first, then process the directory after.
It's missing the -execdir option! As stated in man pages of find:
-execdir command {};
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.
find . -depth -type d -name 'thefoldername*' -execdir mv {} newfoldername \;
With the previous answer my folders contents are disappeared.
This is my solution. It works well:
for i in find -type d -name 'oldFolderName';
do
dirname=$(dirname "$i")
mv $dirname/oldFolderName $dirname/newFolderName
done
.../ABC -> .../BCD
find . -depth -type d -name 'ABC' -execdir mv {} $(dirname $i)/BCD \;
Replace 1100 with old_value and 2200 with new_value that you want to replace.
example
for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
Proof
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 1100
[user#server test]$ for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
[user#server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 2200
here __ in sed is used only to change the name it have no other significance

Resources