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
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
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
To exit the manual you can hit
q
,:q
,Q
,:Q
orZZ
.
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
rmdir
The rmdir
command is used to remove an empty directory.
rmdir stand for remove directories.
rmdir name_of_directory
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
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/
open
The open
command is used to open a file or a directory
open name_of_file
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)
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 intoll
.First thing's first, assuming you're using
bash/sh
and notzsh
or variants (usewhich $SHELL
to figure that out!), runnano ~/.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 tosource ~/.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, whilstcommand && command
will run each individually assuming the last command succeeds.command &
will leave the task running indefinitely, so be careful (you can alwaysCtrl + C
out of it).Or you can press
Ctrl + Z
after you docommand &
to run it as a background job.Using
open
like that is an OS X/macOS thing -- the FreeDesktop.org standard most Linux distributions adhere to is to usexdg-open
.On Linux, the
open
command handles virtual terminals.Oh thank you, I didn't know. I note it
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 ..
Nice post! Just a note though,
open
command is a little different between Mac OS and Linux distributions. Linux equivalent isxdg-open
.Edit: You guys already noticed it :(
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 withreturn
orenter
. 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.
grep
is awesome way to search for stuff.Particularly when combined with
|
operator to chain stuff.For example,
ls | grep my_file.txt
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 backcd -
takes you to the previous directoryls -sh
lists the files/directories with their sizesls -l
lists the details including permissions.There are lots more, but add these to yours
Have fun in your journey!
I really do love
tar -xfv test.tar
andls -lisa
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)
I typically use ls -lart :) (the a isn't necessary but it is appropriate ;) )
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
isls -larth
.If I understand correctly, you can chain options?
-xfv
means-x
, then-f
, then-v
, is it correct?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.If you want to check a file quickly without having to deal with
vim/emacs/nano
and it's too long to usecat/echo
,more
is really useful but it only reads in one direction. For both directions check outless
to read both directionsI'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:
| When you want to delete a non-empty directory, use with caution!ls -a | Basically like ls, but also displays hidden files.
rm -rf
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.
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.
save yourself.
unlearn rmdir.
now.
There's something wrong with
rmdir
?nope, nothing at all
my bad
I saw "rmdir" and read "rm -rf"
rm -rf
does incredible and irreparable damage except you have backups handy.You can't restore a backup if you
rm -rf /
.Exactly. You just wiped out the entire system..
Well except if you have an external backup of some sort. 😏
I think
cat
andwhich
are extremely usefulOh
which
command is very useful indeed, especially on macOS where paths are hidden. Thanks, I discovered!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)
Although you should really be using fd
Oh yeah, good to know, thanks!