简体   繁体   中英

Linux find command questions

I do not have a working Linux system to try these commands out with so I am asking on here if what I am planning on doing is the correct thing to do. (Doing this while I am downloading an ISO via a connection that I think dial-up is faster).

1, I am trying to find all files with the .log extension in the /var/log directory and sub-directories, writing the standard out to logdata.txt and standard out to logerrors.txt

I believe the command would be:

$ find /var/log/ -name *.log 1>logdata.txt 2>/home/username/logs/logerrors.txt.

2, Find all files with .conf in the /etc directory. standard out will be a file called etcdata and standard error to etcerrors.

$ find /etc -name *.conf 1>etcdata 2>etcerrors

3, find all files that have been modified in the last 30 minutes in the /var directory. standard out is to go into vardata and errors into varerrors.

Would that be:

$ find /var -mmin 30 1>vardata 2>varerrors.

Are these correct? If not what am I doing wrong?

1, I am trying to find all files with the .log extension in the /var/log directory and sub-directories, writing the standard out to logdata.txt and standard out to logerrors.txt

Here you go:

find /var/log/ -name '*.log' >logdata.txt 2>/home/username/logs/logerrors.txt

Notes:

  • You need to quote '*.log', otherwise the shell will expand them before passing to find .
  • No need to write 1>file , >file is enough

2, Find all files with .conf in the /etc directory. standard out will be a file called etcdata and standard error to etcerrors.

As earlier:

find /etc -name \*.conf >etcdata 2>etcerrors

Here I escaped the * another way, for the sake of an example. This is equivalent to '*.conf' .

3, find all files that have been modified in the last 30 minutes in the /var directory. standard out is to go into vardata and errors into varerrors.

find /var -mmin -30 >vardata 2>varerrors

I changed -mmin 30 to -mmin -30 . This way it matches files modified within 30 minutes. Otherwise it matches files were modified exactly 30 minutes ago.

When using wildcards in the command, you need to make sure that they do not get interpreted by the shell. So, it is better to include the expression with wildcards in quotes. Thus, the first one will be:

find /var/log/ -name "*.log" 1>logdata.txt 2>/home/username/logs/logerrors.txt

Same comment on the second one where you should have "*.conf" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM