The command line makes our life so much easier since we can automate several mundane tasks and make things run smoother. Instead of clicking around in the Graphical User Interface (GUI), we can fire off a couple of commands and call it job done.
A Unix shell is a command-line interpreter or shell that provides a command-line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language and is used by the operating system to control the execution of the system using shell scripts.
Every Linux or Mac-based operating system has a command-line installed by default, usually under the name “Terminal.” The command line (CLI) lets us easily move and rename files, sort through data, and navigate around the computer.
Without further ado, here are 11 command line tricks that will make your life easier.
1. grep
$ grep "some string" file
The grep command searches for patterns in each file. It also looks for patterns separated by newline characters, and grep prints each line that matches a pattern.
The -i option enables us to search for a string case-insensitively in the given file. It matches words like “REACT,” “REact,” and “react.”
$ grep -i "REact" file
We can find the number of lines that matches the given string/pattern with the -c (count) flag.
$ grep -c "react" index.js
Here’s a fun and an educational comic about the grep command I found on the internet.
In addition, the variant programs egrep and fgrep are the same as
grep -E and grep -F, respectively. These variants are deprecated,
but are provided for backward compatibility.
You can do a lot with grep— read the documentation for an in-depth dive here.
2. ls
$ ls
ls lists files and directories in the current active path. If the pathname is a file, ls displays information on the file according to the requested options. If the pathname is a directory, ls displays information on the files and subdirectories therein.
You might have noticed the files are being shown as a grey color, while the folders are blueish. This is to help us make a distinction between folders and files.
3. pwd
$ pwd
The pwd command is a command-line utility for printing the current working directory. The output will print the full system path of the current working directory to standard output. By default, the pwd command ignores symlinks, although the full physical path of a current directory can be shown with an option.
4. cat
$ cat somefile.js
The cat command has three related functions with regard to a text file:
- Displaying them
- Combining copies
- Creating new ones
The most common use of cat is to read the contents of files, and cat is often the most convenient program for this purpose.
In the following example, the standard output of cat is redirected using the output redirection operator (which is represented by a rightward pointing angular bracket) to file2:
$ cat somefile > somefile2
5. echo
$ echo "some text"
The echo command in Linux is used to display a line of text/string that’s passed as an argument. The echo is a built-in command that’s mostly used in shell scripts and batch files to output status text to the screen or a file.
6. touch
$ touch somefile
The touch command is used to create a file without any content. The touch command can be used when the user doesn’t have data to store at the time of file creation.
Notice how we’re using touch to create the file and cat to look inside the file. Since the newly created index2.js file is empty, the cat returns nothing.
Here are the main differences between cat andtouch:
- cat— Used to create the file with the content.
- touch— Creates a file without any content or empty files. Remember, the file created using touch command is empty. This command is useful when the user doesn’t have data to store at the time of file creation.
7. mkdir
$ mkdir some-directory
As you guessed it, mkdir creates a new empty directory in the current active path. Instead of clicking around in your text editor or GUI, use this command to create new folders.
Note: Notice how we can peek inside the directory with the previous ls command.
7.1 rm
$ rm someFile
Rm stands for remove, which does exactly what it says it does. Removes, or in other words, deletes a file.
By default, the rm command doesn’t remove directories. You need to pass the -rf flag to remove directories.
$ rm -rf some-directory
Note: This removes the directory unconditionally, whether the directory has content inside or not.
7.2 rmdir
$ rmdir some-directory
The rmdir command removes the directory if there’s no content inside the directory.
8. tail
$ tail somefile
The tail command reads a file and outputs the last part of it (the “tail”).
The tail command is useful when going through crash reports or previous history logs. Here’s an example of its usefulness when working with file logs.
# tail /var/log/messages
Mar 20 12:42:22 hameda1d1c dhclient\[4334\]: DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x280436dd)
Mar 20 12:42:24 hameda1d1c avahi-daemon\[2027\]: Registering new address record for fe80::4639:c4ff:fe53:4908 on eth0.\*.
Mar 20 12:42:28 hameda1d1c dhclient\[4334\]: DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x280436dd)
Mar 20 12:42:28 hameda1d1c dhclient\[4334\]: DHCPACK from 10.76.198.1 (xid=0x280436dd)
Mar 20 12:42:30 hameda1d1c avahi-daemon\[2027\]: Joining mDNS multicast group on interface eth0.IPv4 with address 10.76.199.87.
Mar 20 12:42:30 hameda1d1c avahi-daemon\[2027\]: New relevant interface eth0.IPv4 for mDNS.
Mar 20 12:42:30 hameda1d1c avahi-daemon\[2027\]: Registering new address record for 10.76.199.87 on eth0.IPv4.
Mar 20 12:42:30 hameda1d1c NET\[4385\]: /sbin/dhclient-script : updated /etc/resolv.conf
Mar 20 12:42:30 hameda1d1c dhclient\[4334\]: bound to 10.76.199.87 -- renewal in 74685 seconds.
Mar 20 12:45:39 hameda1d1c kernel: usb 3-7: USB disconnect, device number 2
9. wget
$ wget someurl
GNU Wget is a free software package for retrieving files using HTTP, HTTPS, FTP, and FTPS — the most widely-used internet protocols. It’s a non-interactive command-line tool, so it may easily be called from scripts, CRON jobs, terminals without X-Windows support, etc.
GNU Wget has many features to make retrieving large files or mirroring entire web or FTP sites easy, including:
- Can resume aborted downloads using REST and RANGE
- Can use filename wild cards and recursively mirror directories
- NLS-based message files for many different languages
- Optionally converts absolute links in downloaded documents to relative so that downloaded documents may link to each other locally
- Runs on most UNIX-like operating systems as well as Microsoft Windows
- Supports HTTP proxies
- Supports HTTP cookies
- Supports persistent HTTP connections
- Unattended / background operation
- Uses local file timestamps to determine whether documents need to be re-downloaded when mirroring
- GNU Wget is distributed under the GNU General Public License.
Read the official GNU documentation for more information.
10. find
$ find path -name filename
The find command lets you quickly lookup a file or directory. It’s useful when you’re working on a big project with hundreds of files and multiple directories.
Search for files of a particular type
The find command also lets you search for the same type of files in a directory (and its subdirectories). For example, the following command will search for all .js files in your current working directory.
$ find . -name "*.js"
11. mv
$ mv somefile /to/some/other/path
The mv command moves files or directories from one place to another. The mv command supports moving single files, multiple files, and directories.
Conclusion
Thanks for reading, I hope you learned something new. If you know a handy command-line trick, please let’s grow together and post it in the responses.
If you want to learn more about shell commands, I suggest grabbing the “Linux for beginners” book. Stay curious and hungry!