LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2015, 10:53 AM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
Excluding existing folders in bash


I have a list of directories in a file:

Code:
/root
/boot
/sys
/a_dir
/b_dir
I want to remove already existing directories from this list:

Code:
/a_dir
/b_dir
How can I do this? I know xargs can deal with one line at a time and [ -d dirname ] will show if the dirname is present. So I tried:

Code:
cat dirlist | xargs -I {} [ -d {} ]
but it does not work. Thanks for your help.

Last edited by rng; 12-08-2015 at 11:04 AM.
 
Old 12-08-2015, 01:48 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I don't understand the question. Why don't you simply delete the unwanted entries using sed?

Code:
$ echo "/root
/boot
/sys
/a_dir
/b_dir" | sed '/\/[a-b]_dir/d'
/root
/boot
/sys
But I am probably missing something...

Best regards,
HMW
 
Old 12-08-2015, 06:54 PM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
The filter should check if that directory exists. If it does not exist, only then its name should be printed, else not. It could be something like:

Code:
cat dirlist | awk 'if( [ ! -d $1 ] ) print ($1)'
How can this awk code be corrected or any other method can be used?

Last edited by rng; 12-08-2015 at 06:55 PM.
 
Old 12-08-2015, 08:13 PM   #4
onlyesterday16
LQ Newbie
 
Registered: Dec 2014
Distribution: Fedora
Posts: 9

Rep: Reputation: Disabled
I think you need use a loop:
Code:
for dir in $(cat dirlist); do
        if [ ! -d $dir ]; then
                do smt
        fi
done

Last edited by onlyesterday16; 12-08-2015 at 08:16 PM.
 
Old 12-08-2015, 09:59 PM   #5
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks. I also managed using awk as follows:

Code:
cat dirlist | awk ' { if ( system(" [ -d " $1 " ] ")  ) print $1 } '
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Move Folders Older than X, excluding one folder firegs Linux - General 4 06-02-2011 02:47 PM
Excluding Hidden Files / Folders in Tarball carlosinfl Linux - General 2 05-22-2008 10:23 AM
excluding folders in a rsync command troubles cucolin@ Linux - Server 18 04-10-2007 10:59 AM
Excluding Folders with CP Luke_C Linux - General 17 07-27-2006 04:22 AM
Excluding specific folders from backup karloslambchop Ubuntu 7 07-21-2006 08:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:44 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
Open Source Consulting | Domain Registration