Friday, June 25, 2021

Get the all folder names in current directory

 I wanted to copy a file from one location to multiple folders. We'll have to copy multiple times if we do it manally. Lets see how to use shell script to copy to every folder in one go using shell scripting.

main parts we need to do.

1. get all directories.

    find . -maxdepth 1 -type d

2. loop through every directory name, returned from 1, and copy file to each folder.

using foreach loop in s shell (.csh) 

> foreach dir ( ` find . -maxdepth 1 -type d` )
> foreach? cp ~/Scripts/convert.py $dir
> foreach? end

using for loop in bash shell (.sh)

Other commands to get all directory names

  1. ls -d */
  2. ls -l | grep `^d'

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...