ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The commands you will need are find, xargs and rm. This exact combination has been discussed many times before in this forum. Have a look at this thread to get a very good idea of what you need to do:
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Just one advice.
At the start of your script, put:
Code:
CMD=echo
Once you have tested and debugged your code, change this to:
Code:
CMD=/bin/rm
In your script use $CMD instead of rm.
You will be debugging with echo statements, after all is tested you replace echo with rm. Otherwise you might wipe out your hard disk when you forget a decimal point or so in your code. Been there, done that.
i need to search for several type of extension .wk1 , .log , .txt etc. how do i incorporate it in? . Another thing is
ls -l | find . -name "*.txt" -mtime +3 -exec ls {} \; it actually returns me all the subdirectories as well. y is that so?
in my office, i have existing scripts that have similiar syntax
find . ! -type d -ctime -5 -name '*.txt' -exec ls {} \;
find . ! -type d -ctime -5 -name '*.txt' -exec rm -f {} \;
You can combine terms with OR and AND logical operators. The find syntax is "-o" and "-a" respectively. For example:
Code:
find . -name '*.txt' -o '*.xls'
Which will match files whose name matches the pattern *.txt OR the pattern *.xls - i.e. it will list them both.
You can also create more complex conditions using ( and ) to group statements. You can also use ! for logical NOT. These operators need to be escaped with a \ to prevent the shell from interpreting them. For example the condition "files whose name does not contain 'eight', and whose name matches *.jpg or *.txt" might be represented with the pseudo code:
Code:
(NOT NAME '*eight*') AND (NAME '*.jpg' OR NAME '*.txt')
hi matthew, i finally make out the command..
KEEP=5
find . ! -type d -mtime +${KEEP} -a \( -name '*.log' -o -name '*.xls' -o -name '*.txt' -o -name '*.wk1'
\) -exec ls {} \;
i guess this should be correct. but the issue is that it will traverse down the subdirectories as well...i just want to limit to the current directory. how do i do that?
another question is...i have a ! ...what does it mean in the statement?
Don't forget to escape the "!" character - many shells use this character to refer to previous commands, and can do weird stuff if you don't escape it with a preceding "\" character.
The -maxdepth option takes an integer argument which says how many levels of sub-directory to descend. 1 means just the specified directory (i.e. do not look in sub-directories. It matters where you put the -maxdepth option - it should go directly after the ".".
Code:
KEEP=5
find . -maxdepth 1 \! -type d -mtime +${KEEP} -a \( -name '*.log' -o -name '*.xls' -o -name '*.txt' -o -name '*.wk1' \) -exec ls {} \;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.