How to get directory list with find command
ls
command shows all the files and directories in the current directory. This command can be used only if the current directory contains only directories.
There are files in most cases. Use find
command in this case.
$ find .
.
./a
./a.txt
./b
./b/aa.txt
./b/bb
./b.txt
Excludes children in the nested directories
Add maxdepth
option to limit the max depth.
$ find . -maxdepth 1
.
./a
./a.txt
./b
./b.txt
The result doesn’t include children in the nested directories.
Excludes files Get only directories
Add -type d
to get only directories.
$ find . -type d
.
./a
./b
./b/bb
If we need to get only files, use -type f
instead.
$ find . -type f
./a.txt
./b/aa.txt
./b.txt
find command to get directires in the current directory
Combine the two options above into one.
$ find . -maxdepth 1 -type d
.
./a
./b
How to use the command result in another command with xargs
How can we use the result of find command? xargs
is one of the solutions. It uses the result to the next command.
$ find . | xargs echo
. ./a ./a.txt ./b ./b/aa.txt ./b/bb ./b.txt
echo
command is executed only once. All paths are used in the same execution.
Restrict the number of arguments per execution
We want to use one argument per execution. Add -n
option in this case.
$ find . | xargs -n 2 echo
. ./a
./a.txt ./b
./b/aa.txt ./b/bb
./b.txt
$ find . | xargs -n 1 echo
.
./a
./a.txt
./b
./b/aa.txt
./b/bb
./b.txt
How to put the result into arbitrary position
If a post-command requires multiple arguments, we must control the place to put the path.
Add -I VARIABLE_NAME
in this case.
$ find . | xargs -n 1 -I FOO echo the path is FOO
xargs: warning: options --max-args and --replace/-I/-i are mutually exclusive, ignoring previous --max-args value
the path is .
the path is ./a
the path is ./a.txt
the path is ./bneed to
the path is ./b/aa.txt
the path is ./b/bb
the path is ./b.txt
Oh, the two options are exclusive. Using -I
option is enough.
$ find . | xargs -I FOO echo the path is FOO
the path is .
the path is ./a
the path is ./a.txt
the path is ./b
the path is ./b/aa.txt
the path is ./b/bb
the path is ./b.txt
Of course, the variable can be used in the middle.
$ find . | xargs -I FOO echo the path FOO looks good
the path . looks good
the path ./a looks good
the path ./a.txt looks good
the path ./b looks good
the path ./b/aa.txt looks good
the path ./b/bb looks good
the path ./b.txt looks good
Git pull from a parent directory
git pull
is usually executed in a git directory. However, if we have many directories to update, it’s tedious to change the current directory. How can we execute a git command from a parent directory?
Add -C path_to_a_repo
to the git command. If we want to execute git pull
for b_repo, the command looks below.
git -C ./b_repo pull
git pull for all repos in the current directory
Let’s combine the all commands. The following command updates all the repositories in the current directory.
find . -maxdepth 1 -type d | xargs -I {} git -C {} pull
Note that this command pulls the latest code from your remote repository for the current branch.
Comments