No matter what sort of development job you have, a good understanding of Linux is essential. In this post, I'll walk through a lot of the basic Linux commands that will help you get your job done quicker and easier. This post assumes zero prior knowledge to Linux or working in a command line environment.
The reason I chose to put the man command first is that once you know this, it'll help you understand all sorts of other commands.
The purpose of man is to display the 'manual page' for a specific command. So, if I was curious what whoami does, I can run
and it'll print information like the usage and different parameter options. I highly recommend you using the man command on a lot of the commands below as you go through them.
cd stands for change directory. Say you're in /user/david and you want to switch to /user/tim, you can simply run
and you'll be moved to that directory. Another useful command along with cd is
pwd, which will tell you what directory you're currently in.
Along with cd, here are some useful shortcuts that get used with cd to help you move even quicker.
cd: just typing cd with no arguments will take you to your home directory (same as
cd ..: moves you one directory back. You can move multiple levels back by adding a slash between the two dots:
cd -: moves you to the last directory you were in
ls gives you a listing of the contents of the directory. Just typing ls will list the contents of the current directory, but you can also pass a directory to ls to list its contents I would run man ls to see all of the different options you can pass, but here are some I find useful.
ls -l: long listing. This will show things like the date and size of the file and have the results formatted a bit nicer
ls -lt: long listing sorted by time with the newest showing at the top
ls -ltr: long listing sorted by reverse time with the newest showing at the bottom
ls -lh: long listing with human readable file sizes
ls -a: list showing hidden files
mv file-name desired-path
mv will move a file to the given path. mv is also used to rename files. Say I have a file called
file1 in my current directory, and I want to rename it to file2. I can simply run
mv file1 file2, which will essentially just rename the file.
cp file-name desired-path
cp is used to make a copy of a file. There's not too much to say on this one, but it gets used a ton. Run
man cp to see some of the options.
File viewing commands
cat will display the contents of a file. You can also use
tac to display the contents of the file in reverse order (pretty clever right?).
head will display the first 10 lines of a particular file. You can also choose how many lines (if you want more or less than 10),
by passing, for example
head -n 20 file-name, which would list the first 20 rows.
Similarly, you can use
tail to view the last 10 lines of the file.
vi is a text editor that can be used on nearly all unix based systems. It is extremely powerful and has a ton of options inside it.
vi could be its own series of posts, so I'll just keep this one brief. Once you have a file opened up in vi, you can make edits to the file
by typing i, then making your inserts. Once you're done, hit the escape key, then
:w to write the file, then
:q to quit vi. You can
also use those together by just typing
:wq which will write the file and quit. If you want to quit without saving your changes,
Other file viewing commands
Here's some more file viewing commands that are available on most systems. I don't use them as often, but I figured I'd throw them out there. Give them a shot if you're interested.
Other commonly used commands
ssh stands for secure shell. This is most commonly used to get terminal access to a remote server or computer. If you're at work and have a development server in your data center, you can use ssh to get terminal access to that server. When using ssh, you can leave user-name blank if your username on the server is the same as the username that you are currently logged in as.
scp file-name username@server-name:desired-directory
scp stands for secure copy and is used to copy files from one server to another. If you want to copy a directory, you can use
scp -r directory-name username@server-name:desired-directory
df is used to see how much disk space is available. The directory name parameter is optional. This is commonly ran as
df -h, which will print the size value in human readable format.
ps is used to get a list of all the processes currently running on the machine. This is commonly ran as
ps aux, which gives you a more complete picture on what is running.
Once you run a ps command and have the pid (process ID), you can kill the process by running
That's about all I have for now. Hopefully this was useful to get a basic understand of Linux commands. Feel free to leave a comment or question below. Thanks for reading!