开发者

Unix: traverse a directory

开发者 https://www.devze.com 2023-04-08 04:12 出处:网络
I need to traverse a directory so starting in one directory and going deeper into difference sub directories. However I also need to be able to have access to each individual file to modify the file.

I need to traverse a directory so starting in one directory and going deeper into difference sub directories. However I also need to be able to have access to each individual file to modify the file. Is there already a command to do this or 开发者_如何学Cwill I have to write a script? Could someone provide some code to help me with this task? Thanks.


The find command is just the tool for that. Its -exec flag or -print0 in combination with xargs -0 allows fine-grained control over what to do with each file.

Example: Replace all foo's by bar's in all files in /tmp and subdirectories.

find /tmp -type f -exec sed -i -e 's/foo/bar/' '{}' ';'


for i in `find` ; do
  if [ -d $i ] ; then do something with a directory ; fi
  if [ -f $i ] ; then do something with a file etc. ; fi
done

This will return the whole tree (recursively) in the current directory in a list that the loop will go through.


This can be easily achieved by mixing find, xargs, sed (or other file modification command).

For example: $ find /path/to/base/dir -type f -name '*.properties' | xargs sed -ie '/^#/d'

This will filter all files with file extension .properties. The xargs command will feed the file path generated by find command into the sed command. The sed command will delete all lines start with # in the files (feed by xargs).

Command combination in this way is very flexible.

For example, find command have different parameters so you can filter by user name, file size, file path (eg: under /test/ subfolder), file modification time.

Another dimension of flexibility is how and what to change in your file. For ex, sed command allows you to make changes on file in applying substitution (specify via regular expressions). Similarly, you can use gzip to compress the file. And so on ...


You would usually use the find command. On Linux, you have the GNU version, of course. It has many extra (and useful) options. Both will allow you to execute a command (eg a shell script) on the files as they are found.

The exact details of how to make changes to the file depend on the change you want to make to the file. That is probably best scripted, with find running the script:

POSIX or GNU:

find . -type f -exec your_script '{}' +

This will run your script once for a group of files with those names provided as arguments. If you want to do it one file at a time, replace the + with ';' (or \;).


I am assuming SearchMe is the example directory name you need to traverse completely.
I am also assuming, since it was not specified, the files you want to modify are all text file. Is this correct?

In such scenario I would suggest using the command:

find SearchMe -type f -exec vi {} \;

If you are not familiar with vi editor, just use another one (nano, emacs, kate, kwrite, gedit, etc.) and it should work as well.


Bash 4+

shopt -s globstar
for file in **
do
    if [ -f "$file" ];then
        # do some processing to your file here
        # where the find command can't do conveniently        
    fi
done
0

精彩评论

暂无评论...
验证码 换一张
取 消