DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Useful Linux Commands — Soft Links and Files

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>
Enter fullscreen mode Exit fullscreen mode

Soft links will be broken with the original file removed.

For instance, we run:

ln -s foo.txt newfoo.txt
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

-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
Enter fullscreen mode Exit fullscreen mode

We can also search with multiple keywords with -or :

find . -type d -name node_modules -or -name public
Enter fullscreen mode Exit fullscreen mode

We can exclude results with -not :

find . -type d -name '*.md' -not -path 'node_modules/*'
Enter fullscreen mode Exit fullscreen mode

And we can search for files with at least with the given size with -size :

find . -type f -size +100c
Enter fullscreen mode Exit fullscreen mode

c means bytes.

We can search for files with size in a range with:

find . -type f -size +100k -size -1M
Enter fullscreen mode Exit fullscreen mode

We can search for files that are edited more than a given number of days ago with -mtime :

find . -type f -mtime +3
Enter fullscreen mode Exit fullscreen mode

And we can delete files that are found with the -delete option:

find . -type f -mtime -1 -delete
Enter fullscreen mode Exit fullscreen mode

cat

cat lets us add content to a file.

To print content to standard output, we run:

cat file
Enter fullscreen mode Exit fullscreen mode

To print the content of multiple files, we run:

cat file1 file2
Enter fullscreen mode Exit fullscreen mode

We can redirect the output to a file by running:

cat file1 file2 > file3
Enter fullscreen mode Exit fullscreen mode

We can change > to >> top create the file if it doesn’t exist.

And we can print line numbers with -n :

cat -n file
Enter fullscreen mode Exit fullscreen mode

touch

The touch command lets us create an empty file.

For instance, we run:

touch file
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to watch foo.txt and update the output.

We can change the number of lines printed with -n :

tail -n 10 <filename>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Conclusion

We can create soft links and create and manipulate files with Linux commands.

Top comments (0)