Linux Commands

1. man:

  • Manual Pages
  • The first command to remember
  • Contains info about almost everything 🙂

–other commands

–system calls

–c/library functions

–other utils, applications, configuration files

  • To read about man itself type:
    % man man
  • NOTE: unfortunately there’s no
    % man woman …

2. which:

  • Displays a path name of a command.
  • Searches a path environmental variable for the command and displays the absolute path.
  • To find which tcsh and bash are actually in use, type:
    % which tcsh
    % which bash
  • % man which for more details

3. chsh:

  • Change Login Shell
  • Login shell is the shell that interprets commands after you logged in by default.
  • You can change it with chsh (provided that your system admin allowed you to do so).
  • To list all possible shells, depending on implementation:
    % chsh -l
    % cat /etc/shells
  • % chsh with no arguments will prompt you for the shell.

4. whereis:

  • Display all locations of a command (or some other binary, man page, or a source file).
  • Searchers all directories to find commands that match whereis’ argument

% whereis tcsh

General Commands

5. passwd:

  • Change your login password.
  • A very good idea after you got a new one.
  • It’s usually a paranoid program asking your password to have at least 6 chars in the password, at least two alphabetical and one numerical characters. Some other restrictions (e.g. dictionary words or previous password similarity) may apply.
  • Depending on a privilege, one can change user’s and group passwords as well as real name, login shell, etc.
  • % man passwd

6. date:

  • Guess what 🙂
  • Displays dates in various formats
  • % date
  • % date -u

– in GMT

% man date

7. cal:

  • Calendar

–for month

–entire year

  • Years range: 1 – 9999
  • No year 0
  • Calendar was corrected in 1752 – removed 11 days
  • % cal  current month
  • % cal 2 2000  Feb 2000, leap year
  • % cal 2 2100  not a leap year
  • % cal 2 2400  leap year
  • % cal 9 1752  11 days skipped
  • % cal 0  error
  • % cal 2002  whole year

8. clear:

  • Clears the screen
  • There’s an alias for it: Ctrl+L
  • Example sequence:

–% cal

–% clear

–% cal

Ctrl+L

9. sleep:

  • “Sleeping” is doing nothing for some time.
  • Usually used for delays in shell scripts.
  • % sleep 2   2 seconds pause

10. Command Grouping:

  • Semicolon: “;”
  • Often grouping acts as if it were a single command, so an output of different commands can be redirected to a file:
  • % (date; cal; date) > out.txt

11. alias:

  • Defined a new name for a command
  • % alias

–with no arguments lists currently active aliases

  • % alias newcommandoldcommand

–defines a newcommand

  • % alias cl cal 2003
  • % cl

12. unalias:

  • Removes alias
  • Requires an argument.
  • % unaliascl

12. history:

  • Display a history of recently used commands
  • % history

–all commands in the history

  • % history 10

–last 10

  • % history -r 10

–reverse order

  • % !!

–repeat last command

  • % !n

–repeat command n in the history

  • % !-1

–repeat last command = !!

  • % !-2

–repeat second last command

  • % !ca

–repeat last command that begins with ‘ca’

13. apropos:

  • Search man pages for a substring.
  • % apropos word
  • Equivalent:
  • % man -k word
  • % apropos date
  • % man -k date
  • % apropos password

14. exit / logout :

  • Exit from your login session.
  • % exit
  • % logout

15. shutdown:

  • Causes system to shutdown or reboot cleanly.
  • May require superuser privileges
  • % shutdown -h now  – stop
  • % shutdown -r now  – reboot

File

16. ls:

  • List directory contents
  • Has whole bunch of options, see man ls for details.
  • % ls

–all files except those starting with a “.”

  • % ls -a

–all

  • % ls -A

–all without “.” and “..”

  • % ls -F

–append “/” to dirs and “*” to executables

  • % ls -l

–long format

  • % ls -al
  • % ls -lt

–sort by modification time (latest – earliest)

  • % ls -ltr

–reverse

17. cat:

  • Display and concatenate files.
  • % cat

–Will read from STDIN and print to STDOT every line you enter.

  • % cat file1 [file2] …

–Will concatenate all files in one and print them to STDOUT

  • % cat > filename

–Will take whatever you type from STDIN and will put it into the file filename

  • To exit cat or cat > filename type Ctrl+D to indicate EOF (End of File).

18. more/ less:

  • Pagers to display contents of large files page by page or scroll line by line up and down.
  • Have a lot of viewing options and search capability.
  • Interactive. To exit: ‘q’

19. less:

  • less (“less is more”) a bit more smart than the more command
  • to display contents of a file:

–% less filename

  • To display line numbers:

–% less -N filename

  • To display a prompt:

–% less -P”Press ‘q’ to quit” filename

  • Combine the two:

–% less -NP”Blah-blah-blah” filename

  • For more information:

–% man less

20. touch:

  • By touching a file you either create it if it did not exists (with 0 length).
  • Or you update it’s last modification and access times.
  • There are options to override the default behavior.
  • % touch file
  • % man touch

21. cp:

  • Copies files / directories.
  • % cp [options] <source> <destination>
  • % cp file1 file2
  • % cp file1 [file2] … /directory
  • Useful option: -i to prevent overwriting existing files and prompt the user to confirm.

22. mv:

  • Moves or renames files/directories.
  • % mv <source> <destination>

–The <source> gets removed

  • % mv file1 dir/
  • % mv file1 file2

–rename

  • % mv file1 file2 dir/
  • % mv dir1 dir2

23. rm:

  • Removes file(s) and/or directories.
  • % rm file1 [file2] …
  • % rm -r dir1 [dir2] …
  • % rm -r file1 dir1 dir2 file4 …

24. script:

  • Writes a log (a typescript) of whatever happened in the terminal to a file.
  • % script [file]
  • % script

–all log is saved into a file named typescript

  • % script file

–all log is saved into a file named file

  • To exit logging, type:

–% exit

25: find:

  • Looks up a file in a directory tree.
  • % find . -name name
  • % find . \(-name ‘w*’ -or -name ‘W*’ \)

26. mkdir:

  • Creates a directory.
  • % mkdirnewdir
  • Often people make an alias of md for it.

27. cd:

  • Changes your current directory to a new one.
  • % cd /some/other/dir

–Absolute path

  • % cdsubdir

–Assuming subdir is in the current directory.

  • % cd

–Returns you to your home directory.

28. pwd:

  • Displays personal working directory, i.e. your current directory.
  • % pwd

29. rmdir:

  • Removes a directory.
  • % rmdirdirname
  • Equivalent:

–% rm -r dirname

30. ln:

  • Symbolic link or a “shortcut” in M$ terminology.
  • % ln –s <real-name> <fake-name>

31. chmod:

  • Changes file permissions
  • Possible invocations

–% chmod 600 filename

–-rw——- 1 user group 2785 Feb 8 14:18 filename
(a bit not intuitive where 600 comes from)

–% chmodu+rw filename
(the same thing, more readable)

–For the assignment:

  • % chmodu+xmyshellscript
    (mysshellscript is now executable)
  • -rwx—— 1 user group 2785 Feb 8 14:18 myshellscript

32. grep:

  • Searches its input for a pattern.
  • The pattern can be a simple substring or a complex regular expression.
  • If a line matches, it’s directed to STDOUT; otherwise, it’s discarded.
  • % echo “blah-foo” | grep blah

–Will print the matching line

  • % echo “blah-foo” | grep zee

–Will not.

  • See a separate grep tutorial.

33. Pipes:

  • What’s a pipe?

–is a method of interprocess communication (IPC)

–in shells a ‘|’ symbol used

–it means that the output of one program (on one side of a pipe) serves as an input for the program on another end.

–a set of “piped” commands is often called a pipeline

  • Why it’s useful?

–Because by combining simple OS utilities one can easily solve more complex tasks

34. Help: http://www.cs.concordia.ca/help/

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s