move files not ending in .sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting move files not ending in .sh
# 8  
Old 04-04-2011
Code:
find . -maxdepth 1 -type f ! -name "*.sh" -exec mv {} /home/user/backup \;

# 9  
Old 04-04-2011
Seems only Chubler XL's script is working the way I want

Hi Currently it seems that only Chubler XLs code is working the way I want

Code:
bash-3.00$ ls  -ltr
total 8
-rw-r--r--   1 vprabhu  deloitte       0 Apr  4 03:14 startServer.sh
-rw-r--r--   1 vprabhu  deloitte       0 Apr  4 03:14 jra_managed1_20110316.jfr
-rw-r--r--   1 vprabhu  deloitte       0 Apr  4 03:14 config.xml.20110316_1
drwxr-xr-x   2 vprabhu  deloitte       3 Apr  4 03:15 security.20110316.stress
-rwxr-xr-x   1 vprabhu  deloitte     213 Apr  4 03:17 ChublerXL.sh

Runnign Chublers code below

Code:
bash-3.00$ ./ChublerXL.sh 
mv config.xml.20110316_1 ./backup
mv jra_managed1_20110316.jfr ./backup

Vistastar your ls statement only prints out the sh scripts with *, I havent tried with mv yet, but would this behavior change if I used the mv?

Code:
bash-3.00$ ls -F | grep -v '.sh$\|/'
ChublerXL.sh*
config.xml.20110316_1
jra_managed1_20110316.jfr
security.20110316.stress/
startServer.sh

I dont think I have ruby on my serverrs
Code:
bash-3.00$ man ruby
No manual entry for ruby.

I cant use the maxdepth option for some reason

Code:
bash-3.00$ find . -maxdepth 1 -type f -name "*[!.sh]"
find: bad option -maxdepth
find: [-H | -L] path-list predicate-list

Any Ideas?

Thanks
# 10  
Old 04-04-2011
Code:
find ./* -prune -type f ! -name \*.sh -exec mv {} /home/user/backup \;

Note the wildcard in the ./* PATH description ... , if you omit it, the -prune option will not behave as you expect.


(make a test without the -exec mv ... command at first so you can ensure that the selection fits with your requirements.

Last edited by ctsgnb; 04-04-2011 at 10:03 AM..
# 11  
Old 04-04-2011
Quote:
Originally Posted by Chubler_XL
Try this - replace echo with actual move when your happy it's working:

Code:
for file in *
do
   [ -f "$file" ] || continue
   case "$file" in
       *.sh)
          # Not to be moved
       ;;
       *)
          echo mv $file ./backup
       ;;
   esac
done


Quote:
Originally Posted by vistastar
One line of command is enough:

Code:
mv `ls -F | grep -v '.sh$\|/'` path/of/backup


Well, Chubler_XL's solution could be written on one line if one was so inclined (there's nothing in the shell grammar that prevents it).

Also, the approaches are not equivalent. Chubler_XL's solution can handle any valid filename (if $file after mv is quoted), regardless of how ludicrous it may be. Your approach, however, could be wrecked by field splitting and pathname expansion after the command substitution, if any of the filenames contain IFS characters (spaces would be the most likely culprit) or glob metacharacters (and asterisks, question marks, brackets).

The -F option to ls will add an asterisk after any shell scripts with an executable bit set (which is to be expected but not a certainty for a .sh file). The grep should probably handle an optional * after .sh.

Lastly, your grep regular expression is underspecified. The ".sh" will not just match names ending in ".sh" but also "ash", "ish", etc. The dot is a metacharacter and needs to be quoted to match itself.

Regards,
Alister

---------- Post updated at 11:17 AM ---------- Previous update was at 11:12 AM ----------

Quote:
Originally Posted by cgkmal
Code:
find . -maxdepth 1 -type f -name "*[!.sh]" # This part finds and prints files without sh extension.

That's incorrect. Bracketed expressions in globs do not work that way. That actually matches any file whose final character is not a "." or and "s" or an "h", and not a file whose name does not end in the string ".sh".

Regards,
Alister

Last edited by alister; 04-04-2011 at 12:34 PM..
This User Gave Thanks to alister For This Post:
# 12  
Old 04-04-2011
Quote:
Originally Posted by alister
Your approach, however, could be wrecked by field splitting and pathname expansion after the command substitution, if any of the filenames contain IFS characters (spaces would be the most likely culprit) or glob metacharacters (and asterisks, question marks, brackets).
This approach also has issues when no *.sh files exist, or when too many exist and the command line limit is exceeded.

Last edited by Chubler_XL; 04-04-2011 at 11:30 PM..
# 13  
Old 04-05-2011
Quote:
Originally Posted by alister
That's incorrect. Bracketed expressions in globs do not work that way.
Alister
But the correct way is?...

Well, taking correction from rdcwayx's post

! -name "*.sh"

Thanks for the observation.


Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. UNIX for Dummies Questions & Answers

General find /grep question: files with lines ending in r

I am trying to find files that have lines in them that end in an r. I have been able to locate files by using the following command: find . -type f -name "*RECORDS"| xargs grep -l r$ However, I now want to find files that don't end in r anywhere. That means that no sentences or lines in... (9 Replies)
Discussion started by: newbie2010
9 Replies

3. Shell Programming and Scripting

Move all files except sys date (today) files in Solaris 10

I want to move all files from one directory to another directory excluding today (sysdate files) on daily basis. file name is in pattern file_2013031801, file_2013031802 etc (2 Replies)
Discussion started by: khattak
2 Replies

4. UNIX for Dummies Questions & Answers

Listing files in a Unix Directory ending with .....

Hi Unix Gurus, I need to list all files in a Unix Directory which either end with a .pdf or .rtf and they should be case insensitive ie .Pdf , .pDF , .RtF etc are also possible. How can i accomplish this with with a ls command ? If not then a find command. (6 Replies)
Discussion started by: pchegoor
6 Replies

5. Shell Programming and Scripting

if statement to check files with different ending but same starting name

I am trying to check if files staring with filename but ending with diffent dates e.g. filename.2011-10-25. The code I am using is below if It works find only if one file is present but returns binary operator expected when there are mulptiple files. Please help me correcting it. I... (5 Replies)
Discussion started by: ningy
5 Replies

6. Homework & Coursework Questions

display certain files ending

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: how to display a listing of all the files ending in txt using one command. 2. Relevant commands, code,... (4 Replies)
Discussion started by: austing5
4 Replies

7. Shell Programming and Scripting

Convert directory of text files to Unix/Linux Line Ending

I need help converting a directory of *.txt with Windows line ending to UTF-8 character encoding and Unix/Linux line ending. (9 Replies)
Discussion started by: chipperuga
9 Replies

8. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

9. Shell Programming and Scripting

Delete files in a folder with a specific ending

Hi I have files that end with .txt.txt that i want to delete. But I also have files that end with .txt that I want to leave intact. How do I specifically delete files that end with .txt.txt in a folder. thanks (5 Replies)
Discussion started by: kylle345
5 Replies

10. Shell Programming and Scripting

How to check files and move the results to differents files?

Hi, I am a newbie to shell scripting. here is my objective: 1)The shell program should take 2 parameters - ie-> DestinationFolder, WebFolder 2)Destination folder contains few files that has to has be verified and deleted. 3)WebFolder is a folder containing a list of master files 4)It... (1 Reply)
Discussion started by: sandhyagupta
1 Replies
Login or Register to Ask a Question