All Posts

Linux Commands Every Developer Should Know

Having spent the majority of my more recent workdays wrangling containers and navigating Linux systems in a variety of fashions it’s become clear to me that there are some commands that every Software Developer working in a unix environment should know. Below are a list of some of those commands that I use on a day to day basis which can be incredibly helpful.

  1. Let’s say we wanted to replace every occurrence of “jeff” in a file, ourfile.txt, and replace it with “ben”. We can complete this task very easily using the command outlined below. There are a few arguments here such as s for case-insensitive, g for global replace and -i for edit in place https://linuxize.com/post/how-to-use-sed-to-find-and-replace-string-in-files/.
# Linux variant
$ sed -i 's/jeff/ben/g' ourfile.txt
# MacOS variant
$ sed -i '' 's/jeff/ben/g' ourfile.txt'
  1. Displaying the contents of a file, especially a short file, has never been easier than using cat. The cat command allows you to dump the contents of a file to the terminal for inspection.
$ cat ourfile.txt
  1. Following cat you also have head and tail which can be used to show the start or end of a file respectively. You can even provide arguments to view a certain number of lines or a specific set of lines within a file. These commands are extremely helpful when working with large log files.
# Get the first 200 lines of a file
$ head -200 ourfile.txt
# Get the last 200 lines of a file
$ tail -200 ourfile.txt
  1. Along with cat, sed can also be used to display a range of line numbers from a file. This is especially handy for those really large files where you need to read a portion of the contents without reaching the buffer limit within your terminal.
# Get the next 300 lines after line 40
$ sed -n '40,300 p' ourfile.txt
  1. One of the simplest ways to move files from remote servers and a local machine is through scp. I use this approach along with another utility, rsync, to move files residing on different machines. This is especially handy for copying down large log files for local inspection.
# Copy a file from your local machine to a remote server
$ scp our-local-file.txt youruser@serverip:/remote/directory
# Copy a file from a remote server to your local machine
$ scp youruser@serverip:/remote/directory ~/Documents
  1. Modifying directory and file ownership and permissions. Ownership and permissions is a much larger subject with a good deal of documentation out there to support your journey. I would absolutely recommend checking out https://www.tutorialspoint.com/unix/unix-file-permission.htm for a primer.
# Change the ownership of a directory and it's subdirectories and files
$ chown -R user:group directory
# Change the ownership of a single file
$ chown user:group directory
# Make a file executable, useful for bash scripts
$ chmod +x yourscript.sh
# Modify permissions so that only the owner can read, write and execute, no one else has permissions
$ chmod 700 yourfile
# Modify permissions so that only the owner can read and write, no one else has permissions
$ chmod 600 yourfile
# Modify permissions so that only the owner can read and write, group and world can only read
$ chmod 644 yourfile
  1. Compress a directory and subdirectories for download from a remote server. On many Linux distributions you will have access to zip. You can easily compress a file for download with a one liner and use the scp command to download that file from a remote server.
# Install the zip utility
$ apt-get install zip
# Zip the directory and it's contents
$ zip -r newfilename.zip yourfolder
  1. Need to run a command on a remote server with an unstable internet connection? If you’ve ever had to execute a long running command on cheap wireless then you know the stress of having your script unexpectedly die when your wifi goes out. nohup is a perfect solution to this issue and will allow you to avoid your script being terminated by the hangup signal. You can read more here: https://linuxize.com/post/linux-nohup-command/.
$ nohup yourscript.sh &