LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2013, 12:39 PM   #1
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 180

Rep: Reputation: Disabled
shell script question about variables


Hi all

---------------------
update: typo in the code, problem fixed. but now i notice a new problem. when i run this command, i meant to remove everything older then 2013-jan-10 (20130110). but it seems to have removed itself and a whole bunch of files instead. the reason why i said it removed itself is because i couldn't find it that directory afterwards.

so the new question is: a mtime of 20130110 would remove everything prior to that or everything on that date or everything post that date?

update 2:
i modified my code to print line by line

[CODE]deleting file number 2 which is named /tmp/delete.sh
Deleting file
[CODE]

IT is somehow removing my files from before that date:

deleting file number 26 which is named /tmp/A20130104.0525-0530
Deleting file

---------------------

I have this script that i have modified, original is found on some linux/unix forum. but when i run it it is giving me errors.

It should do two things: based on a time of the file, give it write permission, and then delete it.

Code:
delcnt=0
for files in `find /path-to-folder -mtime -20130110 -print`
do
   echo "Deleting file $file"
   chmod +w $files
   rm $files
   delcnt=$(($delcnt + 1))
done

echo "deleted $delcnt files"
the result is this:
Deleting file
chmod: missing operand after `+w'
Try `chmod --help' for more information.
rm: missing operand
Try `rm --help' for more information.


So i know that somehow, the $file variable is empty but i don't know why.

thanks guys

Last edited by jzoudavy; 08-16-2013 at 01:05 PM. Reason: question changed
 
Old 08-16-2013, 12:40 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
you are calling out the list as $files, then calling out the list in the do loop as $file.

So,.. just change all of your variables to be the same.

Code:
delcnt=0
for file in `find /path-to-folder -mtime -20130110 -print`
do
   echo "Deleting file $file"
   chmod +w $file
   rm $file
   delcnt=$(($delcnt + 1))
done

echo "deleted $delcnt files"

Last edited by szboardstretcher; 08-16-2013 at 12:42 PM.
 
1 members found this post helpful.
Old 08-16-2013, 12:45 PM   #3
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 180

Original Poster
Rep: Reputation: Disabled
eh... ops... thanks :P
 
Old 08-16-2013, 05:17 PM   #4
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
why do you chmod +w before deleting?
i think you dont need it.

Quote:
$ ls
$ mkdir sampledir
$ touch samplefile
$ ls -l
total 4
drwxr-xr-x 2 engineer engineer 4096 Ağu 17 00:06 sampledir
-rw-r--r-- 1 engineer engineer 0 Ağu 17 00:06 samplefile
$ chmod -w sample*
$ ls -l
total 4
dr-xr-xr-x 2 engineer engineer 4096 Ağu 17 00:06 sampledir
-r--r--r-- 1 engineer engineer 0 Ağu 17 00:06 samplefile
$ rm -f samplefile
$ rm -fr sampledir/
$ ls -l
total 0
 
Old 08-16-2013, 05:30 PM   #5
climaxnix
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian GNU/Linux
Posts: 12

Rep: Reputation: 2
$file - $files typo only causes problem in echo. Because it is written $files in other part of the code and therefore it deletes your files.

but i think you have to focus on "-mtime -20130110" option.
Taking from man find:

Quote:
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the inter‐
pretation of file modification times.
So as you could see you dont give a exact date but the number of days.
as an example for it:
-mtime -10 => files that are less than 10 days old. ie. files that is 0,1,2,....or 9 days old.

-mtime +10 => files that are are more than 10 days old. i.e files tht is 11,12,13... days old.

-mtime 10 => files that are exactly 10 days old.

So i think "-mtime -20130110" is interpreted as files 0,1,..... or 20130110 days old! and I gues they are all the files in the folder.
 
Old 08-16-2013, 06:29 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Also, you don't need a script to do that. The execdir action should do the trick:
Warning: Untested code: find /path-to-folder -maxdepth 1 -type f -mtime +10 -execdir rm -f {} ';'


For example, this will run the ls action on all non-hidden files in my current directory more that 30 days old:
Code:
$ find ./ -maxdepth 1 -type f -mtime +30 ! -name '.*' -ls
1969990   12 -rw-r--r--   1 Peter    Peter        9235 Oct  2  2012 ./conkyrc-wminfo
1969979   12 -rw-rw-r--   1 Peter    Peter       11126 Oct  2  2012 ./conkyrc-brenden.txt
1969989   16 -rw-rw-r--   1 Peter    Peter       14471 Oct  2  2012 ./conkyrc-vert.txt
1970727    4 -rw-rw-r--   1 Peter    Peter         264 Oct 11  2012 ./PTrenholme.revoke
1969373  108 -rw-rw-r--   1 Peter    Peter      105922 Oct  2  2012 ./conky-dlab.tar.gz
 
1 members found this post helpful.
Old 08-16-2013, 08:58 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Exclamation

Quote:
Originally Posted by PTrenholme View Post
Also, you don't need a script to do that. The execdir action should do the trick:
Warning: Untested code: find /path-to-folder -maxdepth 1 -type f -mtime +10 -execdir rm -f {} ';'


For example, this will run the ls action on all non-hidden files in my current directory more that 30 days old:
Code:
$ find ./ -maxdepth 1 -type f -mtime +30 ! -name '.*' -ls
1969990   12 -rw-r--r--   1 Peter    Peter        9235 Oct  2  2012 ./conkyrc-wminfo
1969979   12 -rw-rw-r--   1 Peter    Peter       11126 Oct  2  2012 ./conkyrc-brenden.txt
1969989   16 -rw-rw-r--   1 Peter    Peter       14471 Oct  2  2012 ./conkyrc-vert.txt
1970727    4 -rw-rw-r--   1 Peter    Peter         264 Oct 11  2012 ./PTrenholme.revoke
1969373  108 -rw-rw-r--   1 Peter    Peter      105922 Oct  2  2012 ./conky-dlab.tar.gz
FYI: I always find it helpful to replace 'rm' with 'ls' when testing commands of this sort.
 
Old 08-16-2013, 11:08 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by szboardstretcher View Post
FYI: I always find it helpful to replace 'rm' with 'ls' when testing commands of this sort.
Um, yes. Please note the example.
 
Old 08-19-2013, 10:36 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Quote:
Originally Posted by PTrenholme View Post
Um, yes. Please note the example.
gotcha. i saw the code in the 'warning untested' tag. just being safe either way.

thanka
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables in SHELL SCRIPT rahulruns Programming 1 11-12-2009 01:52 AM
Shell script question - Lynx + input variables viceroy Programming 2 07-22-2007 08:25 PM
Shell script variables Fredde87 Linux - Newbie 13 11-01-2006 04:35 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
Why?? can not use variables with shell script Bassam Linux - General 9 01-27-2004 08:42 AM


All times are GMT -5. The time now is 04:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration