The Tricky.net

The basics
The basics



The basic commands
The basics

It's always difficult for new linux users to find the basic commands, without having all the hundreds of useless options in the syntax.

I try here to summarize the basic and most usefull commands with examples (don't you always look for examples?).

Don't hesitate to leave a comment for any other commands suggestions!

Manage folders and files

cd - Change Directory

move to the specified directory

example :


 

cd /var/logs

 

ls - List the content of the current directory

example :

ls

Just type ls to have a list.

Usefull options

  • Display the details of the files, like date and rights : ls -l
  • Display the hidden files : ls -a

Note you can always combine two options like that :

ls -al

 

mv - MoVe a file or rename it

move a file to another place, OR rename a file. This command will rename a file if both arguments are not path

example

mv myFile.js myNewFile.js

The file called myFile.js will be renamed to myNewFiles.js

If now you specify a path for one of the argument, mv will try to move the file.

example :

mv myFile.js myFolder/myFile.js

Note you don't have to re-type the name of your file, you could write:

mv myFile.js myFolder/

 

mkdir - Make a Directory

create a directory in the current folder.

example :

mkdir myNewDir


rm - ReMove a file or a directory

Removes a single file. You cannot remove a folder with rm, unless you use the option -r (Recursive) : warning, all the files contained by the folder will be removed!

examples :

rm myFile.js

rm -r myFolder/

Usefull options

  • Remove a directory with all its content : rm -r (Recursive)
  • Remove a directory containing read-only files : rm -rf (Recursive and Force removing)

Note that to remove a file beginningwith '-', you cannot escape the name (like "\-myFile.js"), you must write something like:

rm ./-myFile.js

 

cp - CoPy a file or a folder

Copy a file. To copy an entire folder, use the -r option (Recursive)

examples :

cp myFile.js myFile_copied.js

cp -r myFolder/ myFolder_copied/

Useful options

  • Copy an entire folder : cp -r (recursive)

du - Disk Usage

Returns the size in kilobytes of the files or folders given in argument.

examples:

du myFile.html


du -h -s *


Useful options:
  • Human-readable size: du -h
  • Returns size for each folder, and not for each files recursively : du -s

About Searching

Searching for files or text on Linux is very powerfull. Using regular expressions is a considerable advantage.

grep - search text in the given file

grep looks for a given text in all the given file. You can use here the star * to look into all the files of the folder. You can also search in all the subfolders by using the -r (Recursive) option.

example :

grep "I look for you" myTextFile.txt
grep "I look for you" *
grep -r "I look for you" myFolder/
grep -r "I look for you" *
The last example will search in every file of the current folder, and in the subfolders as well.

Note that grep doesn't search in file names! (grep -r "myFile.js" won't return you the path to this file!)

Useful options

  • Recursive search in all subfolders : grep -r note you can also often use the equivalent rgrep
  • Invert the command, look for lines which does not contain the given string : grep -v

 

Combining some commands

Using the pipe (|)

Use the pipe operator to build a "super" command! I explain : imagine you want to search a previous command you typed some time ago, a cp for example. The command history will display a list of the last typed command. How can you now get this list, and then search your command in it ? Here comes the pipe operator into play: you would write

history | grep "cp"
See the trick?

The result of the first command history is given to the second command grep : grep will search in the list returned by history.

Now you can build some super commands, like :

  • searching in search results, i.e. search for multiple terms :
    rgrep "my text" * | grep "my second text"
  • more to come...
 



Fly.net Artisan maçon