Bash to list all folders in a specific directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to list all folders in a specific directory
# 1  
Old 10-31-2016
Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the folder names. Thank you Smilie.

Bash
Code:
#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
for file in ${dir}/*
  do  
    echo "$file"
  done > /home/cmccabe/Desktop/tetsfolder/list

folders in /home/cmccabe/Desktop/tetsfolder
Code:
f1
f2
f3

current output
Code:
/home/cmccabe/Desktop/tetsfolder/f1
/home/cmccabe/Desktop/tetsfolder/f2
/home/cmccabe/Desktop/tetsfolder/f3
/home/cmccabe/Desktop/tetsfolder/list

desired output
Code:
f1
f2
f3

# 2  
Old 10-31-2016
Hi,

can you try the below one ?

Code:
A="/home/cmccabe/Desktop/tetsfolder/f1"
echo ${A##*/}

Gives desired output
Quote:
f1
Seems i misinterpret it. If you want to print only directories, following might help:
Code:
find /home/cmccabe/Desktop/tetsfolder -type d -exec basename {} \;


Last edited by greet_sed; 10-31-2016 at 12:06 PM.. Reason: Add solution to print directory only
# 3  
Old 10-31-2016
ls -dF1 */

Options may be differents depending on your OS (check man ls)
# 4  
Old 11-01-2016
Code:
#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
cd ${dir}
for file in *
  do  
    echo "$file"
  done > /home/cmccabe/Desktop/tetsfolder/list

# 5  
Old 11-01-2016
Perhaps a neater way if this is available:-
Code:
pushd $dir
ls -1
popd



Does this help?



Robin
# 6  
Old 11-01-2016
hi, try:
Code:
#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
for file in ${dir}/*/
  do
    file="${file%%/}"  
    echo "${file##.*/}"
  done > /home/cmccabe/Desktop/tetsfolder/list

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create automated scan of specific directory using bash

I am trying to use bash to automate the scan of a specific directory using clamav. Having this in place is a network requirement. The below is an attempt to: 1. count the extensions (.txt, .jpeg) in a directory and write them to a virus-scan.log (section in bold) 2. scan each folder in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Bash directory loop, but only choose those folders with specific word in it

Hello, how in bash i can get directory loop, but only choose those folders with specific word in it, so it will only echo those with specific word #!/bin/bash for filename in /home/test/* do if ; then echo $filename; fithx! (4 Replies)
Discussion started by: ZerO13
4 Replies

3. Shell Programming and Scripting

Bash to add folder to exsisting folders in directory

I am trying to create subdirectories in each folder in /home/cmccabe/Desktop/NGS/test/*. When I use echo I can see each folder in the directory, but I can not seem to add the commented out portion in bold. These are the sub-directories and sub-folders I am trying to add to each folder in... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

5. Shell Programming and Scripting

Move specific folders and subfolders in a directory

I am trying to move specific folders and subfolders within a directory using the below. I can see the folders to move and they are at the location, but I am getting an error. Thank you :). mv -v /home/cmccabe/Desktop/NGS/API/6-10-2016{bam/{validation,coverage},bedtools /media/cmccabe/"My... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

8. Solaris

Limit bash/sh user's access to a specific directory

Hello Team, I have Solaris 10 u6 I have a user test1 using bash that belong to the group staff. I would like to restrict this user to navigate only in his home directory and his subfolders but not not move out to other directories. How can I do it ? Thanks in advance (1 Reply)
Discussion started by: csierra
1 Replies

9. Shell Programming and Scripting

Bash to download specific files and save in two folders

I am trying to download all files from a user authentication, password protected https site, with a particular extension (.bam). The files are ~20GB each and I am not sure if the below is the best way to do it. I am also not sure how to direct the downloaded files to a folder as well as external... (7 Replies)
Discussion started by: cmccabe
7 Replies

10. Solaris

List all files that contain a specific directory

I need to list all files and subdirectories that contain "oradata". For example, I have several files in several different directories that contain "oradata". I.e. /u07/oradata/1.dbf /u09/unix/whatever/oradata/2.xxx That is, whatever file on the system that contains a directory called... (7 Replies)
Discussion started by: Sat510
7 Replies
Login or Register to Ask a Question