Shell Programming Blog Shell Programming Blog

Tuesday, 2 October 2012

Linux Shell Scripting: how to list the files alone in a directory

Unknown | 09:21 |


Bash Linux Unix shell scripting

 Often we tend to look into the directories to check what files are present in the directory. When we try to list the files using ls command, all files along with the directories will be displayed. What if we want to list only the files and not the directories???

We will see how to do that in this post.

1) Using "find" command:

$ find . -type f -maxdepth 1

The above command will list also the hidden files. But if you want to avoid the hidden files to be displayed in your results, you need to use the below command.

$ find . -type f -maxdepth 1 \( ! -iname ".*" \)


2) To derive it from the "ls" command, we can use the below commands.

$ ls -l | grep -v ^d

$ ls -l | awk 'NR!=1 && !/^d/ {print $NF}'


0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming