LinuxQuestions.org
Register a domain and help support LQ
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 10-11-2011, 06:54 PM   #1
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
Adding additional commands to a bash shell script


I have a simple script that removes old files from a directory:

#! /bin/bash
set -o errexit
set -o nounset

# Get a list of files that meet the critera for removal.


for file in `find <My_Directory> -type f -mtime +2`;
do rm -f $file;
done

However, I want to add the following to it:

for file in *;
do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`;
done

Is it possible and if so how can I do it?
 
Old 10-11-2011, 08:45 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
I am not sure I see the issue here? I would guess that the files being removed are not the ones being renamed, so simply add the new code??
 
Old 10-11-2011, 08:47 PM   #3
Ian John Locke II
Member
 
Registered: Mar 2008
Location: /dev/null
Distribution: Slackware, Android, Slackware64
Posts: 130

Rep: Reputation: 17
If you do not want to remove the files you can do
Code:
#!/bin/bash

set -o errexit
set -o nounset

for file in *; do 
   mv "$file" "$(echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g')"
done
If you want to keep the removal of the files, I have no clue what you're asking.
 
Old 10-11-2011, 09:40 PM   #4
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
I have discovered that some of the files in the directory have spaces. They need to be deleted as well, but I cannot think of a way to do that straight up.
 
Old 10-11-2011, 09:47 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
find should return the entire fname, inc spaces. Put double quotes around it for the rm cmd
Code:
for file in `find <My_Directory> -type f -mtime +2`;
do 
    rm -f "$file"
done
NB: *nix does not have an undo, so test carefully...
 
Old 10-12-2011, 01:07 AM   #6
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by chrism01 View Post
find should return the entire fname, inc spaces. Put double quotes around it for the rm cmd
Code:
for file in `find <My_Directory> -type f -mtime +2`;
do 
    rm -f "$file"
done
NB: *nix does not have an undo, so test carefully...
Thats not a good idea if you have files with spaces. Consider:
Code:
$ ls -ltr
total 8
-rwxr-xr-x   1 nikeop   algo          58 Sep 14 03:33 check_log.sh
-rw-rw-r--   1 nikeop   algo           0 Oct 11 10:35 hello world
drwxrwxr-x   2 nikeop   algo        4096 Oct 11 10:36 tmp
Then:
Code:
$ for i in `find . -type f` ; do
> ls -l "$i"
> done
-rwxr-xr-x   1 nikeop   algo          58 Sep 14 03:33 ./check_log.sh
./hello: No such file or directory
world: No such file or directory
Better:
Code:
$ find . -type f |while read i ; do
> ls -l "$i"
> done
-rwxr-xr-x   1 nikeop   algo          58 Sep 14 03:33 ./check_log.sh
-rw-rw-r--   1 nikeop   algo           0 Oct 11 10:35 ./hello world
 
Old 10-12-2011, 08:49 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

As rikxik has demonstrated, the secret is to not use a for loop, but a while+read loop instead, possibly while also changing the delimiter to null.

http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001

Edit:
Also, you could just do the whole deletion operation with find. GNU find even has a -delete option built-in. But you can also use the -exec option to call rm, or pipe the command into xargs (again, using the null separator to handle files with spaces).

Here are a couple of links about find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Edit2:

The move operation can likely be made cleaner too, using parameter expansion.

Code:
for file in *;
	newfile="${file// \*/_}"
	newfile="${newfile//_-_/-}"

	mv "$file" "$newfile"
done
A for loop can be used here, since globbing expansion happens after word-splitting is finished.

I'm not 100% I understand the purpose of the sed expression 's/ */_/g' here though. Are you trying to replace a literal space+asterix string (as I set it for above)? Because as written, sed treats it as a regex "zero or more spaces", and thus inserts underscores between every character. If that's what you want, then unfortunately the parameter expansion alone wouldn't work, because it uses the asterix as a globbing character, rather than a regex operator.

Last edited by David the H.; 10-12-2011 at 09:11 AM. Reason: addendum
 
  


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
adding commands to a limited shell program khoh Programming 3 04-04-2011 10:50 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Passing commands through an SSH shell in a bash script buee Linux - Newbie 12 07-01-2010 02:28 AM
Adding Commands to Shell metallica1973 Solaris / OpenSolaris 15 12-21-2007 03:17 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM


All times are GMT -5. The time now is 04:45 AM.

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