Running the Find command in the background

dfad123

New Member
Joined
Jul 26, 2021
Messages
8
Reaction score
0
Credits
78
Hello,

I am going through a Linux course and was asked to run the find command in the background and it should list all files that have changed in the last 5 days.

I am running the following command, but it ignores the & to background it -
find / -mtime -5 &

I am also trying to suspend the command while it's running, but it just ignores my ^z

Any help is appreciated.

Thank you
 


The output is very long so I shortened it by grepping for txt

If I run find -mtime -5 and add the &, it just outputs to the screen.

Also, if I run find -mtime -5 >TestFile.txt it outputs to the screen.

If you run any of these commands on your system, does it run in the background or output to a text file.

I am just trying to figure out if the find command is meant to ignore the background & and redirect output to a file.

Thx

Dan

Output:

dan@pop-os:~$ find -mtime -5 | grep txt
./.cache/tracker/locale-for-miner-apps.txt
./.mozilla/firefox/rkeltqtd.default-release/SiteSecurityServiceState.txt
./.mozilla/firefox/rkeltqtd.default-release/AlternateServices.txt
./.mozilla/firefox/rkeltqtd.default-release/serviceworker.txt
./output.txt
./findCommandoutput.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/571988c4897ef87c20ec4d6b13878cf6c36e1caf/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/e01f19e2630bf857ca9222cb547db7dc458ce71f/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/5e32900ad7c1a895555fd05d8041fb6b584ad539/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/84b597b297dddc7f60222cd32da139dd05316ac0/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/cca3f0d9ad2c35cb718c129cd3407b7abf527ae2/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/379f1cbab5b08b6fc9e08681e42d8be311441c88/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/8cbb992fe0cd9ef960e69a214646bd270516a23e/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/2d5d77554141a7026d7415b7b3df1676ed8c4afb/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/eadf114e35641d8a14aa9648d8e1c01b4b3bb3f0/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/4fb0d2c66b84395852cceac05c59792751bead9c/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/579544fd7d0441717f082c9eb123588966aa57ac/index.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/stage.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/browsers.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/CDP/DefaultBackupSet/cpentries.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/.trace/traceLog.txt
./IDriveForLinux/idriveIt/user_profile/root/.trace/traceLog.txt
 
I am also trying to suspend the command while it's running, but it just ignores my ^z
Use CTRL-C instead. It is more common to interrupt a process.


I think this old article may explain the trouble. I don't run anything in "background" so I hope that the article explains it well enough and is still accurate.

These commands run fine for me.
Code:
sudo find / -mtime -5  # This outputs everything to the console
Code:
sudo find / -mtime -5 > /home/stan/Desktop/testfile.txt
# This saves a LARGE text file (over 10 MB) but errors still output to the console
 
Use CTRL-C instead. It is more common to interrupt a process.


I think this old article may explain the trouble. I don't run anything in "background" so I hope that the article explains it well enough and is still accurate.

These commands run fine for me.
Code:
sudo find / -mtime -5  # This outputs everything to the console
Code:
sudo find / -mtime -5 > /home/stan/Desktop/testfile.txt
# This saves a LARGE text file (over 10 MB) but errors still output to the console

If the OPs command is running in the background, then Ctrl+C and Ctrl+Z won’t help.
To stop a process running in the background, you have to use the kill command and send a signal.
The syntax is:
Code:
kill {options} {signal} {pid}
Where {options} are any options you might want to use.
{signal} is the signal you want to send to the application.
And {pid} is the pid of the process you want to control.

So to stop/pause the command you’d send a SIGSTOP signal (-19).

To resume a command, you’d send SIGCONT (-18).

And to kill the process entirely - you should use SIGTERM (-15), which tells the application to wrap things up and shut itself down.
This shuts processes down cleanly and prevents any loss, or corruption of data.

And if sending signal -15 fails to kill the process, the last resort is to send the SIGKILL (-9) signal, which will kill the process dead, regardless of what it’s doing. Which runs a risk of possible data loss, or corruption in any files that the process may have been writing to.

When you run a command in the background, the shell will output the pid (process ID) of the background task.
If you forget the pid, or it scrolls off the screen with any output from the command, you can use the jobs command to list any running background jobs.

E.g.
Bash:
jobs -l

And that will show you the pid’s of all background jobs that are running in the current shell.

If the job is running in another shell, you’d need to use something like pgrep to identify the pid.

Once you have the pid of the background process, you can send a signal to stop it, like this:
Bash:
kill -19 12345
Where 12345 is the pid of the process we want to stop. And hopefully, the process will stop!

To resume the process, repeat the above command and send -18 instead of -19.

Likewise, to kill the process - send -15 (or -9) as a last resort.

You can specify the signals as SIGSTOP, SIGCONT, SIGTERM, or SIGKILL, but I prefer to use their numbers, because it’s a few less characters to type! Ha ha!
 
The output is very long so I shortened it by grepping for txt

If I run find -mtime -5 and add the &, it just outputs to the screen.

Also, if I run find -mtime -5 >TestFile.txt it outputs to the screen.

If you run any of these commands on your system, does it run in the background or output to a text file.

I am just trying to figure out if the find command is meant to ignore the background & and redirect output to a file.

Thx

Dan

Output:

dan@pop-os:~$ find -mtime -5 | grep txt
./.cache/tracker/locale-for-miner-apps.txt
./.mozilla/firefox/rkeltqtd.default-release/SiteSecurityServiceState.txt
./.mozilla/firefox/rkeltqtd.default-release/AlternateServices.txt
./.mozilla/firefox/rkeltqtd.default-release/serviceworker.txt
./output.txt
./findCommandoutput.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/571988c4897ef87c20ec4d6b13878cf6c36e1caf/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/e01f19e2630bf857ca9222cb547db7dc458ce71f/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/5e32900ad7c1a895555fd05d8041fb6b584ad539/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/84b597b297dddc7f60222cd32da139dd05316ac0/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/cca3f0d9ad2c35cb718c129cd3407b7abf527ae2/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/379f1cbab5b08b6fc9e08681e42d8be311441c88/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/8cbb992fe0cd9ef960e69a214646bd270516a23e/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/2d5d77554141a7026d7415b7b3df1676ed8c4afb/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/eadf114e35641d8a14aa9648d8e1c01b4b3bb3f0/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/4fb0d2c66b84395852cceac05c59792751bead9c/index.txt
./.config/google-chrome/Default/Service Worker/CacheStorage/579544fd7d0441717f082c9eb123588966aa57ac/index.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/stage.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/browsers.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/CDP/DefaultBackupSet/cpentries.txt
./IDriveForLinux/idriveIt/user_profile/dan/dan_f/.trace/traceLog.txt
./IDriveForLinux/idriveIt/user_profile/root/.trace/traceLog.txt

Rather than finding all file-system items and then grepping for .txt - simply tell find to look for .txt files with the required mtime.
Try this:
Bash:
find -type f -iname "*.txt" -mtime -5 > /path/to/outputfile 2> /dev/null &
Note: change /path/to/outputfile to whatever you want to call the output file.
E.g. ~/Desktop/textfile-search-results

The above code starts a job in the background, which runs find, telling it to search for all text files with an mtime of -5 and outputs the results to a file.
The 2> redirects any error messages to /dev/null.

That way, the only thing that should get echoed to the terminal is the pid of the background process.

The ampersand at the end of the command is what specifies that the command should be ran in the background.
 
Last edited:
Moving this to Command Line.

Chris Turner
wizardfromoz
 
Use CTRL-C instead. It is more common to interrupt a process.


I think this old article may explain the trouble. I don't run anything in "background" so I hope that the article explains it well enough and is still accurate.

These commands run fine for me.
Code:
sudo find / -mtime -5  # This outputs everything to the console
Code:
sudo find / -mtime -5 > /home/stan/Desktop/testfile.txt
# This saves a LARGE text file (over 10 MB) but errors still output to the console
Thank you very much
 
Rather than finding all file-system items and then grepping for .txt - simply tell find to look for .txt files with the required mtime.
Try this:
Bash:
find -type f -iname "*.txt" -mtime -5 > /path/to/outputfile 2> /dev/null &
Note: change /path/to/outputfile to whatever you want to call the output file.
E.g. ~/Desktop/textfile-search-results

The above code starts a job in the background, which runs find, telling it to search for all text files with an mtime of -5 and outputs the results to a file.
The 2> redirects any error messages to /dev/null.

That way, the only thing that should get echoed to the terminal is the pid of the background process.

The ampersand at the end of the command is what specifies that the command should be ran in the background.
Thank you.
 

Staff online


Top