How to Copy, move, rename, and remove files in Linux
->
< Copying >
To copy files, you use the cp
command. The following will copy file
to file2
. Note that if file2
doesn't exist, it'll be created, but if it exists, it'll be overwritten:
$ cp file file2
There aren't any undo commands in the Linux CLI, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i
option ("interactive") with cp
. The following does the same as the above, but if file2
exists, you'll be prompted before overwriting:
$ cp -i file file2
cp: overwrite `file2'? n
$
So it's a good idea to use the -i
option whenever you're dealing with important files you don't want to lose!
If you want to copy file
into directory dir1
:
$ cp file dir1
The following would do the same as the above, copy file
into dir1
, but under a different name:
$ cp file dir1/file2
You can also copy multiple files into one directory with a single command:
$ cp file1 file2 file3 dir1
Note that if the last argument isn't a directory name, you'll get an error message complaining about it.
< Moving and renaming >
The mv
command can be used for moving or renaming files. To rename a file, you can use it like this:
$ mv file file2
If file2
doesn't exist, it'll be created, but if it exists, it'll be overwritten. If you want to be prompted before overwriting files, you can use the -i
option the same way as with cp
:
$ mv -i file file2
mv: overwrite `file2'? y
$
To move the file into another directory:
$ mv file dir1
If you want to rename the file to file2
and move it into another directory, you probably already figured out the command:
$ mv file dir1/file2
< Removing files >
The rm
command is used for removing files and directories. To remove a file:
$ rm file
If you use the -i
option, you'll be prompted before removing the file:
$ rm -i file
You can also delete more files at once:
rm file1 file2
Be careful with the rm
command! As I already told you, Linux doesn't have any undo commands, and it doesn't put files into Trash where you can save them later. Once you've deleted a file.
Ubuntu, fedora, centos, linux
Ubuntu / Debian Linux: Install Monit Linux Server Monitoring Utility
->
How do I install monit to monitor my server under Debian / Ubuntu Linux?
Monit is a utility for managing and monitoring processes, files, directories and devices on a Debian / Ubuntu Linux server system. Here a few common uses of monit:
- Monit can start a process if it does not run
- Restart a process if it does not respond
- Stop a process if it uses to much resources etc
How do I install monit utility for monitoring services?
Type the following command as the root user:
$ sudo apt-get update $ sudo apt-get install monit
Configure monit
Open monit configuration file /etc/monit/monitrc using vi text editor or nano command in linux:
# vi /etc/monit/monitrc
OR
# nano /etc/monit/monitrc
You need to set following parameters:
set daemon 120 set logfile syslog facility log_daemon set mailserver localhost # primary mailserver set alert vivek@nixcraft.com # receive all alerts
The next step is to save and close this file. Where,
- set daemon 120 : Start monit in background as daemon and check the services at 2-minute intervals.
- set logfile syslog facility log_daemon : Log messages in /var/log/messsages file
- set mailserver localhost : Send email alert via localmail server such as sendmail. Set list of mailservers for alert delivery. Multiple servers may be specified using comma separator. By default monit uses port 25 - it is possible to override it with the PORT option.
- set alert vivek@nixcraft.com : You can set the alert recipients here, which will receive the alert for each service. The event alerts may be restricted using the list.
Now open /etc/default/monit file to turn on monit service:
# vi /etc/default/monit
OR
# nano /etc/default/monit
Set startup to 1, so monit can start:
startup=1
Save and close the file.
Start the monit Linux monitor tool / service:
# /etc/init.d/monit start
Ubuntu Linux Start / Restart / Stop Apache 2.2 Web Server
Q. How to restart or stop Apache 2.2 web server under Ubuntu Linux?
Task: Start Apache 2.2 Server
# /etc/init.d/apache2 start
or
$ sudo /etc/init.d/apache2 start
Task: Restart Apache 2.2 Server
# /etc/init.d/apache2 restart
or
$ sudo /etc/init.d/apache2 restart
Task: Stop Apache 2.2 Server
# /etc/init.d/apache2 stop
or
$ sudo /etc/init.d/apache2 stop