DEV Community

Cover image for Basic UNIX commands
Jérémy Levron
Jérémy Levron

Posted on • Updated on

Basic UNIX commands

Lately, I started learning commands lines. I mean, I used npm install or gulp, but no more.

So I started using commands lines and I have to say that I like it. A lot.

I wanted to share with you what I learned.

cd

The cd command is used to change the current directory.

cd stand for change directory

cd path/of/directory
Enter fullscreen mode Exit fullscreen mode

To move up by one directory you can hit cd .. Source

ls

The ls command list all files of a type other than directory, found in the current directory.

ls stand for list segments

ls
Enter fullscreen mode Exit fullscreen mode

man

The man command display the online manual pages. We can specify a section as parameter. The section is normally the command, function or file name.

man stand for manual.

man name_of_command
Enter fullscreen mode Exit fullscreen mode

To exit the manual you can hit q, :q, Q, :Q or ZZ.

mkdir

The mkdir command is used to create a new directory. The directory would be create in the current directory.

mkdir stand for make directories.

mkdir name_of_directory
Enter fullscreen mode Exit fullscreen mode

rmdir

The rmdir command is used to remove an empty directory.

rmdir stand for remove directories.

rmdir name_of_directory
Enter fullscreen mode Exit fullscreen mode

We can pass an -p option. In this case each directory argument will be treated as a single path and will be removed if they are empty.

rmdir -p name/of/directory
Enter fullscreen mode Exit fullscreen mode

mv

The mv command is used to move a file into a given directory.

mv stand for move.

mv name_of_file name/of/directory/destination/
Enter fullscreen mode Exit fullscreen mode

open

The open command is used to open a file or a directory

open name_of_file
Enter fullscreen mode Exit fullscreen mode

I know, it's very basic, but you have to start somewhere. 😅

I created a Gist where I add what I learn.

And you, what are your favorite commands?

Top comments (30)

Collapse
 
vsanjo profile image
David Woodward • Edited

If you're finding yourself attacking those annoying commands often, consider setting an alias for them!

Instead of typing ls -AbcClhoG, you could alias all that into ll.

First thing's first, assuming you're using bash/sh and not zsh or variants (use which $SHELL to figure that out!), run nano ~/.bashrc to start editing your config for the shell.

You'll then want to add an alias at the bottom now, using alias ll="ls -AbcClhoG". Easy!

Ctrl + C, Y, Y to save and exit nano, and you'll want to source ~/.bashrc or make a new tab in Terminal to reload your settings.


You can run any commands and name them however you want - even chaining them as you go.

command & command will run them concurrently, whilst command && command will run each individually assuming the last command succeeds. command & will leave the task running indefinitely, so be careful (you can always Ctrl + C out of it).

Collapse
 
cannuhlar profile image
Can Nuhlar • Edited

Or you can press Ctrl + Z after you do command & to run it as a background job.

Collapse
 
ikirker profile image
Ian Kirker • Edited

Using open like that is an OS X/macOS thing -- the FreeDesktop.org standard most Linux distributions adhere to is to use xdg-open.

On Linux, the open command handles virtual terminals.

Collapse
 
19h47 profile image
Jérémy Levron

Oh thank you, I didn't know. I note it

Collapse
 
pbkarlsson profile image
Philip Karlsson

You mentioned the -p flag for rmdir. It can also be used with mkdir, as 'mkdir x/y/z'.

As a few people have mentioned, you can add aliases in your ~/.bashrc.

My favorite is:
mkdcd() {
mkdir $1
cd $1
}
Which makes creating and changing your cwd easy.

And also: cd.. for cd ..

Collapse
 
cannuhlar profile image
Can Nuhlar • Edited

Nice post! Just a note though, open command is a little different between Mac OS and Linux distributions. Linux equivalent is xdg-open.

Edit: You guys already noticed it :(

Collapse
 
emcain profile image
Emily Cain • Edited

Great post Jérémy!

Some more:

ps aux will show you the running processes.

netstat will show you the open network ports.

Both of these will have a very large output, so combine with grep to filter for a specific string:

$ ps aux | grep mysql is a good way to see what process represents a MySQL instance, for example.

NOTE: the $ is conventionally used to represent the start of a command you type into your bash terminal and run with return or enter. You don't type out the $ character.

This is useful when your application is not running as expected and you want to check than any requried processes have been started.

You can use this to figure out what is running on a particular port (and kill it if desired):

stackoverflow.com/questions/115835...

I end up using these a lot when working with introductory web development students -- they don't know how to check what is running, end up starting 2 or more instances of a server process, and then get confused when they can't connect to their web application.

Collapse
 
krishraghuram profile image
krishraghuram

grep is awesome way to search for stuff.

Particularly when combined with | operator to chain stuff.

For example, ls | grep my_file.txt

Collapse
 
prhomhyse profile image
Promise Akpan • Edited

Awesome start Jeremy. When I played with Unix terminal for the first time, I made a major mistake which was not going over the basics.

Add these especially if you are in a directory

cd .. takes you back
cd - takes you to the previous directory

ls -sh lists the files/directories with their sizes
ls -l lists the details including permissions.

There are lots more, but add these to yours

Have fun in your journey!

Collapse
 
neradev profile image
Moritz Schramm

I really do love tar -xfv test.tar and ls -lisa

Collapse
 
michaeltd profile image
michaeltd

If you find your self puzzled by the various archive extensions then this is for you. Linux CLI unpack unification script.

pastebin.com/wNCP166T

(Excuse the shameless plug btw)

Collapse
 
carlschelin profile image
Carl Schelin

I typically use ls -lart :) (the a isn't necessary but it is appropriate ;) )

Collapse
 
moopet profile image
Ben Sinclair

I understand why you do that and it spoils it a little to add a letter - but my only common use case apart from a normal ls is ls -larth.

Collapse
 
19h47 profile image
Jérémy Levron • Edited

If I understand correctly, you can chain options? -xfv means -x, then -f, then -v, is it correct?

Collapse
 
vsanjo profile image
David Woodward

This is correct (of most commands)! Some more complex ones can't be chained.

For example, you can't chain --this-is-an-arg and --this-too-is-an-arg but individual letters are usually fine.

Collapse
 
dpells02 profile image
David Pellegrini

If you want to check a file quickly without having to deal with vim/emacs/nano and it's too long to use cat/echo, more is really useful but it only reads in one direction. For both directions check out less to read both directions

Collapse
 
pbkarlsson profile image
Philip Karlsson

I'd like to recommend this NPM module: npmjs.com/package/tldr

It's basically like the 'man' command, but usually with a shorter description and example uses. For example, 'tldr tar' will tell you which flag combination you need to extract different types of compressed archives.

And I'll add another few commands that I use daily:
ls -a | Basically like ls, but also displays hidden files.
rm -rf

| When you want to delete a non-empty directory, use with caution!
mkdir dir/{subdir1,subdir2} | Use curly braces to create multiple subdirs at once.
cd | Just typing cd is the same as 'cd ~', which brings you to your home directory.
cat | Will print to contents of a file to the console, so you don't have to open the file just to view it.

Also, check out your .bashrc by typing cat ~/.bashrc, in there you'll find several bash aliases, as well as having the possibility to create your own.

And again, don't use rm -rf, if you're not 100% certain that you know what you're doing.

Collapse
 
speedstream profile image
Aaron Santos

Some time ago, I build a .sh where I put all my little scripts to semi-automitize some task: open certain programs with my configs, create my projects with git and makefile, update/upgrade my system, compress files... Other more complex to remember, like connecting to an external monitor via VGA/HDMI. 15 or 20 lil'scripts to make my life easier. Why? If there is a easiest way to do something, I will choose it.

Collapse
 
carywreams profile image
Cary Reams

save yourself.
unlearn rmdir.
now.

Collapse
 
19h47 profile image
Jérémy Levron • Edited

There's something wrong with rmdir?

Collapse
 
carywreams profile image
Cary Reams

nope, nothing at all

my bad

I saw "rmdir" and read "rm -rf"

Thread Thread
 
prhomhyse profile image
Promise Akpan

rm -rf does incredible and irreparable damage except you have backups handy.

Thread Thread
 
andrejnaumovski profile image
Andrej Naumovski • Edited

You can't restore a backup if you rm -rf /.

Thread Thread
 
prhomhyse profile image
Promise Akpan

Exactly. You just wiped out the entire system..
Well except if you have an external backup of some sort. 😏

Collapse
 
andrewchou profile image
Andrew Chou

I think cat and which are extremely useful

Collapse
 
19h47 profile image
Jérémy Levron

Oh which command is very useful indeed, especially on macOS where paths are hidden. Thanks, I discovered!

Collapse
 
andreanidouglas profile image
Douglas R Andreani

The find command is one of the most useful on your toolkit

find . -exec cp {} . \;
(Recusevely find all files in a path and copy to the root)

Collapse
 
legolord208 profile image
jD91mZM2

Although you should really be using fd

Collapse
 
19h47 profile image
Jérémy Levron

Oh yeah, good to know, thanks!