Filter directories from list of files and directories
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file containing full path names of files an directories.
From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.
Can anybody think of an elegant solution?
linux command-line path xargs filter
add a comment |Â
up vote
1
down vote
favorite
I have a file containing full path names of files an directories.
From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.
Can anybody think of an elegant solution?
linux command-line path xargs filter
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file containing full path names of files an directories.
From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.
Can anybody think of an elegant solution?
linux command-line path xargs filter
I have a file containing full path names of files an directories.
From this list I would like to filter out any pathnames that reference directories so that I am left with a list containing only file paths.
Can anybody think of an elegant solution?
linux command-line path xargs filter
asked Jan 14 at 22:06
ARF
1336
1336
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
while IFS= read -r file; do
[ -d "$file" ] || printf '%sn' "$file"
done <input_file
Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):
the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"
file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%sn' "$file"
file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"
etc.
add a comment |Â
up vote
0
down vote
Assuming the file
contains one file path per line, with zsh
:
printf '%sn' $(f)^"$(<file)"(N^/)
would print those that do exist (that we can tell exist) and are not determined to be of type directory (change /
to -/
to also discard symlinks to directories).
Since you mention xargs
, with the GNU implementation (for -d
and -r
), and assuming none of the file paths start with -
or are find
predicates (like !
) you could do:
xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file
Replace -type
with -xtype
(assuming GNU find
) to also exclude symlinks to directories.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
while IFS= read -r file; do
[ -d "$file" ] || printf '%sn' "$file"
done <input_file
Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):
the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"
file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%sn' "$file"
file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"
etc.
add a comment |Â
up vote
3
down vote
accepted
while IFS= read -r file; do
[ -d "$file" ] || printf '%sn' "$file"
done <input_file
Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):
the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"
file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%sn' "$file"
file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"
etc.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
while IFS= read -r file; do
[ -d "$file" ] || printf '%sn' "$file"
done <input_file
Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):
the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"
file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%sn' "$file"
file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"
etc.
while IFS= read -r file; do
[ -d "$file" ] || printf '%sn' "$file"
done <input_file
Would print the files that are not determined to be of type directory (or symlink to directory). It would leave all other type of files (regular, symlink (except to directories), sockets, pipes...) and those for which the type cannot be determined (for instance because they don't exist or are in directories which you don't have search permission to).
Some variations depending on what you meant by file and directory (directory is one of many types of files on Unix):
the file exists (after symlink resolution) and is not of type directory:
[ -e "$file" ] && [ ! -d "$file" ] && printf '%sn' "$file"
file exists and is a regular file (after symlink resolution):
[ -f "$file" ] && printf '%sn' "$file"
file exists and is a regular file before symlink resolution (excludes symlinks):
[ -f "$file" ] && [ ! -L "$file" ] && printf '%sn' "$file"
etc.
edited Jan 15 at 8:12
Stéphane Chazelas
281k53518849
281k53518849
answered Jan 14 at 22:15
Hauke Laging
53.4k1282130
53.4k1282130
add a comment |Â
add a comment |Â
up vote
0
down vote
Assuming the file
contains one file path per line, with zsh
:
printf '%sn' $(f)^"$(<file)"(N^/)
would print those that do exist (that we can tell exist) and are not determined to be of type directory (change /
to -/
to also discard symlinks to directories).
Since you mention xargs
, with the GNU implementation (for -d
and -r
), and assuming none of the file paths start with -
or are find
predicates (like !
) you could do:
xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file
Replace -type
with -xtype
(assuming GNU find
) to also exclude symlinks to directories.
add a comment |Â
up vote
0
down vote
Assuming the file
contains one file path per line, with zsh
:
printf '%sn' $(f)^"$(<file)"(N^/)
would print those that do exist (that we can tell exist) and are not determined to be of type directory (change /
to -/
to also discard symlinks to directories).
Since you mention xargs
, with the GNU implementation (for -d
and -r
), and assuming none of the file paths start with -
or are find
predicates (like !
) you could do:
xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file
Replace -type
with -xtype
(assuming GNU find
) to also exclude symlinks to directories.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Assuming the file
contains one file path per line, with zsh
:
printf '%sn' $(f)^"$(<file)"(N^/)
would print those that do exist (that we can tell exist) and are not determined to be of type directory (change /
to -/
to also discard symlinks to directories).
Since you mention xargs
, with the GNU implementation (for -d
and -r
), and assuming none of the file paths start with -
or are find
predicates (like !
) you could do:
xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file
Replace -type
with -xtype
(assuming GNU find
) to also exclude symlinks to directories.
Assuming the file
contains one file path per line, with zsh
:
printf '%sn' $(f)^"$(<file)"(N^/)
would print those that do exist (that we can tell exist) and are not determined to be of type directory (change /
to -/
to also discard symlinks to directories).
Since you mention xargs
, with the GNU implementation (for -d
and -r
), and assuming none of the file paths start with -
or are find
predicates (like !
) you could do:
xargs -rd 'n' sh -xc 'exec find "$@" -prune ! -type d' sh < file
Replace -type
with -xtype
(assuming GNU find
) to also exclude symlinks to directories.
edited Jan 15 at 7:32
answered Jan 14 at 23:20
Stéphane Chazelas
281k53518849
281k53518849
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417123%2ffilter-directories-from-list-of-files-and-directories%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password