Solved Listing first level non-dot subdirectories only?

mefizto

Well-Known Member

Reaction score: 9
Messages: 420

Greetings all,

as the title says. The following code excludes the .subdirectories:
Code:
find /home/directory/* ! -name /home/directory -prune -type d > subdirectory.txt
, but the subdirectory.txt still contains the parent directory:
Code:
/home/directory/subdirectory_1
/home/directory/subdirectory_2
.
/home/directory/subdirectory_n
, instead of:
Code:
subdirectory_1
subdirectory_2
.
subdirectory_n
.

Any help would be appreciated.

Kindest regards,

M
 

k.jacker

Aspiring Daemon

Reaction score: 331
Messages: 567

Is it this you want...?
Code:
find /home/directory/* ! -name /home/directory -prune -type d | cut -d "/" -f 4- > subdirectory.txt
...adjust to -f 3- as you like.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 6,961
Messages: 28,942

find /home/directory/* ! -name /home/directory -prune -type d -exec basename {} \; > subdirectory.txt
 
OP
OP
M

mefizto

Well-Known Member

Reaction score: 9
Messages: 420

Hi k.jacker, SirDice,

thank you, both alternatives worked. I understand that k.jacker pipes the output of find to cut, while SirDice uses an exec option of the find.

Thus a bigger question is, how did you find the solutions, especially SirDice's? I have been reeding the find(1), but would never understood how to use the exec option in this manner.

Kindest regards,

M
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 6,961
Messages: 28,942

Thus a bigger question is, how did you find the solutions, especially SirDice's?
It helps if you've been writing shell scripts for the past 20 or so years ;)
I have been reeding the find(1), but would never understood how to use the exec option in this manner.
The -exec is fairly simple to understand, it simply executes every time something is found, the {} is a placeholder for found result. The \; is to indicate the end of the -exec parameter.
In this case it executes
basename "/home/directory/subdirectory_1";
basename "/home/directory/subdirectory_2";
etc.

See basename(1).
 
Top