How to check if a file exists on the filesystem.
To check whether a file exists in a Linux filesystem, we can deploy the use of the following commonly used file operators, that is -e
and -f
-e
will check whether a file exists regardless of the type, and will return true if any type of file exists.
-f
will only return true only if the file is a regular file I.e, not a directory or a device.
We use the following syntax
test -e filename
[ e filename ]
test -f filename
[ -f filename ]
An example. Let’s check if we have a filename called shell in the /etc/ directory.
[ -e /etc/shell ] && echo “Found” || echo “Not Found”
[ -f etc/shell ] && echo “Found” || echo “Not Found”
If you run the following you will get an answer, if there is a file matching what you have specified then it will echo Found
and if the file is not available it will echo not found
.
Find the 50th line of a file using tail and head commands.
The Head command
will always output the first 10 items on the file while the Tail command
will print the last 10 items in the file.
If you want to find the 50th line of a file, we need to combine the two, tail and head commands.
Let’s say I have a spreadsheet called students.csv and this list contains 800 students, to display the 50th student we use the following command.
Tail -n +50 students.csv | head -n 1
head -n 50 students.csv | tail -n 1
-n
shows the nth line
+50
tells us to print from line 50. This is an option to be used with the tail command.
Head -n 1
will tell us to print the first line only and that is the 50th line. If you want to print the first 5 lines you use head -n 5 or tail -n 5
How to copy a file from one Linux machine to another
To copy a file from one Linux machine to another, use scp command
which is the secure copy command. The command will look like this:
scp [email protected]:
The filename is the file you want to copy to the remote machine
The user is the destination username
The host is the destination host, and the best way to determine the host is to use the IP address of that host.
The file destination is the directory you want your file to be sent to.
Let’s see an example, We have a file called students.csv on the local machine, to transfer this file we use the following command.
scp students.csv [email protected]:/remote/directory
This will prompt you to authenticate, fill in your password and you are good to go
If you check on your remote server you will see the students.csv file
To copy a directory to a remote machine we use the following command.
scp -r test [email protected]:/remote/directory
The test is the directory I want to copy.
How to monitor a continuously updating log file
The most effective tool to manage continuously changing log files is the tail command
.
The tail command shows the last 10 commands when used, in this case, if logs are continuously changing we can use the tail with -f option will print to the terminal any new lines added to the log file in real-time.
Let’s take an example, we need to monitor apache2, this is how we can see the most recent logs.
Tail -f
sudo tail -f /var/log/apache2/access.log
With this command, we will be in a position to get the latest logs as it occurs.