19
Using Linux Commands 2

Using Linux Commands 2

  • Upload
    nico

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Using Linux Commands 2 . Sort command. Sort the lines of text files . $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z ). Options - PowerPoint PPT Presentation

Citation preview

Page 1: Using Linux Commands 2

Using Linux Commands 2

Page 2: Using Linux Commands 2

2

Sort command

• Sort the lines of text files.$ sort fileName• by default it will sort in normal order(alphabetical 0-9 A-Z ).

• Options• -r Reverse normal order(reverse alphabetical Z-A 9-0).

-n Sort in numeric order(Sorts by the beginning of the number at the beginning of the line.)(A-Z 0-9)-nr Sort in reverse numeric order(9-0 Z-A)

• Note that this command does not sort the actual file, it just displays the sorted output on your terminal.

Page 3: Using Linux Commands 2
Page 4: Using Linux Commands 2
Page 5: Using Linux Commands 2

5

Sequential commands

• To run a sequence of commands type several commands on the same command line and separating them with semicolons(;)

$ mkdir ng ; cd ng ; touch tom

Page 6: Using Linux Commands 2

6

date commands

Change or set current date and time.Syntax date [date or time string ]Examples• Show current date & time

• Set date to 2001-Mar-15

• Set date as well as time

$ date

$date --date=“2001-3-15”

$date --date=“2001-3-15 11:59 AM”

Page 7: Using Linux Commands 2

7

Locating commands• Commands resides in directories.

• The path consists of a list of directories that are checked sequentially for the commands you enter.(When you type a command to run, the system looks for it in the directories specified by PATH in the order specified)

• To display all command’s paths

• To display the command’s locations

• -a ( if the command reside in several location )

Directory Content DirectoryContains common Linux user commands such as ls, sort /bin Contains a variety of other user and administrative commands. /usr Contains administrative commands. /sbin

$ echo $PATH/usr/local/bin:/bin:/usr/bin:usr/X11R6/bin:/home/chris/bin

$ type command

Graphical commands (that are used with GUIs)

The last directory shown is the bin directory in the user's home directory.

Page 8: Using Linux Commands 2

•If you know the directory that contains the command you want to run, one way to run it is to type the full path to that command. For example, you run the date command from the /bin directory by typing:

• $/bin/date Of course, this can be inconvenient, especially if the command resides in a directory with a long name. The better way is to have commands stored in well-known directories, and then add those directories to your shell's PATH environment variable. The path consists of a list of directories that are checked sequentially for the commands you enter. To see your current path, type the following:

• $echo $PATH/bin:/usr/bin:/usr/local/bin:/usr/bin/X11:/usr/X11R6/bin:/home/chris/bin The results show the default path for a regular Linux user. Directories in the path list are separated by colons. Most user commands that come with Linux are stored in the /bin, /usr/bin, or /usr/local/bin directories. Graphical commands (that are used with GUIs) are contained in /usr/bin/X11 and /usr/X11R6/bin directories. The last directory shown is the bin directory in the user's home directory.

Page 9: Using Linux Commands 2
Page 10: Using Linux Commands 2

10

Common shell environment variables

• $HOME: your home directory

• $PATH: list of directories used to find commands.

• $BASH: Contains the full path name of the bash command.

• $PS1: Sets the value of your shell prompt.

• $MAIL: This is the location of your mailbox file.

• $PWD: This is the directory that is assigned as your current directory.

• $OLDPWD: The directory that was the working directory before you changed to the current working directory.

• $RANDOM: Accessing this variable causes a random number to be generated.

• $UID: The user ID number assigned to your user name.

Page 11: Using Linux Commands 2
Page 12: Using Linux Commands 2

12

Rerunning commands: command line completion

$ echo $PA<tab>$ ls ~/uben<tab>

$ ty <tab>

Some of the values you can type partially <Tab>:Environment variables text begin with $User name text begin with ~Commands, aliases, or function text begin with regular char.

Page 13: Using Linux Commands 2

13

Rerunning commands: command line recall

• Type a command line shell’s history list history file• To view history list:

• Run command number (!command number)

• Run previous command

$ history $ history 8

$ !22

$ !!

The last 8 commands, including history 8

If the command # 22 is pwd pwd will be run

Page 14: Using Linux Commands 2

14

Setting your prompt :

$ export PS1=“[\t \w] \$”

[20:26:32 /var/spool] $

Prompt consists of a set of characters that appear each time the shell is ready to accept a command.

\[ precedes a sequence of nonprinting characters. \] Follows a sequence of nonprinting characters. \t prints the current time in hours, minutes, and seconds. \w display the full path to the current working directory. \$ shows the user prompt ($).

Page 15: Using Linux Commands 2
Page 16: Using Linux Commands 2

16

Adding Alias• makes it possible to launch any command or group of commands by

entering a pre-set string of characters.• it allows a user to create simple names or abbreviations (even consisting

of just a single character) for commands regardless of how complex the original commands are and then use them in the same way that ordinary commands are used.

$ alias p=‘pw d; ls -al’$ alias rm =‘rm –i’$ unalias p$ alias del=‘rm –I’ $ alias copy=‘cp’$ alias rename=‘mv’ $ alias md=‘mkdir’

-i interactive. With this option, rm prompts for confirmation before removing any files.

Use the command unalias to remove alias

Page 17: Using Linux Commands 2
Page 18: Using Linux Commands 2
Page 19: Using Linux Commands 2

The order in which the shell checks for commands

1 -Aliases— Names set by the alias command that represent a particular command and a set of options. (Type alias to see what aliases are set.)

2 -Shell reserved word— Words that are reserved by the shell for special use. Many of these are words that you would use in programming-

type functions, such as do, while, case, and else.3 -Function— A set of commands that are executed together within the current

shell.

4 -Built-in commands— A command that is built into the shell.

5 -File system commands— This is a command that is stored in and executed from the computer's file system. (These are the commands that are indicated by the value of the PATH variable.)