11 Console Commands Every Developer Should Know
by MrBr - November 19, 2019 at 10:28 AM
#1
[Image: 0*ya4dNTm8Mvfui1fD]

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.


https://miro.medium.com/max/890/1*ovG5it...dOgclA.png


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


https://miro.medium.com/max/782/1*-KL9Uv...fHNBoQ.png

Here’s a fun and an educational comic about the grep command I found on the internet.

https://miro.medium.com/max/1200/1*Zkpsz..._Ldmw.jpeg

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.


https://miro.medium.com/max/1124/1*JthCA...BJlRIw.png

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

https://miro.medium.com/max/874/1*E1zXuI...0wZ_nw.png

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


https://miro.medium.com/max/1072/1*SL7g1...YBySkQ.png

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


https://miro.medium.com/max/1148/1*NxLOM...dr1f7Q.png

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.


https://miro.medium.com/max/1044/1*1bMjV...MMdDKA.png

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.

https://miro.medium.com/max/764/1*RIrUCS...y9ighQ.png

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 and touch:

  • 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.

https://miro.medium.com/max/1092/1*FsxDB...w0My7A.png

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.

https://miro.medium.com/max/1074/1*damLE...FELVNg.png

By default, the rm command doesn’t remove directories. You need to pass the -rf flag to remove directories.

$ rm -rf some-directory

https://miro.medium.com/max/1136/1*gXTLA...Dw83WQ.png

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.


https://miro.medium.com/max/940/1*aUJ0Vb...kMxmWA.png

8. tail

$ tail somefile

The tail command reads a file and outputs the last part of it (the “tail”).

https://miro.medium.com/max/1402/1*Xvo2z...F0TLTg.png

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.

https://miro.medium.com/max/1440/1*nbzEh...oQbszQ.png

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.

https://miro.medium.com/max/848/1*j291SZ...ipX6sg.png

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"

https://miro.medium.com/max/996/1*_i8_TE...F1JQmA.png


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.

https://miro.medium.com/max/1180/1*Tdqoy...7cfyfQ.png

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.


Wish you best of luck.
source
#2
Nothing new for me, but this is a quality post, thanks
#3
Let's not forget the powerful awk to filter columns
awk -F ',' '{print $1}' file.csv
-F = separator
$1 number of column to sho
#4
Also "sed" to add/remove/replace strings Wink
#5
I made this comment from terminal thanks to this tutorial.


I didn't really, but this shit is really good to know!

Possibly Related Threads…
Thread Author Replies Views Last Post
Anyone who know more about the GoDaddy breach? emoji47 13 927 November 29, 2021 at 09:40 PM
Last Post: whitetantra
Should i open a scam report? Ringer 7 310 November 21, 2021 at 01:11 AM
Last Post: John McAfee
POLL - - Should I do this? Hexikos 10 426 October 30, 2021 at 04:37 AM
Last Post: 0w0

 Users browsing this thread: 1 Guest(s)