Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Linux is an operating system that many developers will use.
Therefore, it’s a good idea to learn some Linux commands.
In this article, we’ll look at some useful Linux commands we should know.
Soft Links
We can create soft links with the ln -s
command.
The general syntax is:
ln -s <original> <link>
Soft links will be broken with the original file removed.
For instance, we run:
ln -s foo.txt newfoo.txt
to create the newfoo.txt
soft link.
We can see that the soft link should have th @
suffix and it’s colored differently when we run the ls -al
command.
find
The find
command lets us find files and folders on the file system.
For instance, we can find all files with the .txt
extension by running:
find . -name '*.txt'
We need quotes around special characters like *
to stop the shell from interpreting them.
We can also he it to find directories with the -type d
switch:
find . -type d -name src
-type f
lets us search for files only.
-type l
lets us search only symbolic libks.
We can also search under multiple root trees:
find folder1 folder2 -name foo.txt
We can also search with multiple keywords with -or
:
find . -type d -name node_modules -or -name public
We can exclude results with -not
:
find . -type d -name '*.md' -not -path 'node_modules/*'
And we can search for files with at least with the given size with -size
:
find . -type f -size +100c
c
means bytes.
We can search for files with size in a range with:
find . -type f -size +100k -size -1M
We can search for files that are edited more than a given number of days ago with -mtime
:
find . -type f -mtime +3
And we can delete files that are found with the -delete
option:
find . -type f -mtime -1 -delete
cat
cat
lets us add content to a file.
To print content to standard output, we run:
cat file
To print the content of multiple files, we run:
cat file1 file2
We can redirect the output to a file by running:
cat file1 file2 > file3
We can change >
to >>
top create the file if it doesn’t exist.
And we can print line numbers with -n
:
cat -n file
touch
The touch
command lets us create an empty file.
For instance, we run:
touch file
to create a file named file
.
If it already exists it opens the file in write mode and the timestamp is updated.
tail
The tail
command lets us show the content at the end of a file,
The -f
switch watches for file changes and updates the output automatically.
For instance, we run:
tail -f /foo.txt
to watch foo.txt
and update the output.
We can change the number of lines printed with -n
:
tail -n 10 <filename>
We print the last 10 lines with -n 10
.
We can print the whole file content starting from a specific line with +
before the line number:
tail -n +10 <filename>
Conclusion
We can create soft links and create and manipulate files with Linux commands.
Top comments (0)