기초 리눅스 명령어
** 기초 리눅스 명령어
- pwd : Present Working Directory. Print Working Directory.
- cd
- cd .. : Move one directory level up.
- cd directory1/directory2 : Navigating through multiple directories.
- cd / : Move to the root directory.
- cd or cd ~ : Navigate to HOME directory. To return to home directory.
- ls : Lists all files and directories in the present working directory.
- ls -R : Lists files in sub-directories as well.
- ls -a : Lists hidden files as well. Hidden files start with '.' period symbol.
- ls -al : Lists files and directories with detailed information.
- cat : Display, Copy, Combine, Create text files.
- cat > filename : Creates a new file.
- cat filename : Displays the file content.
- cat file1 file2 > file3 : Joins two files(file1, file2) and stores the output in a new file(file3).
- mv file "new file path" : Moves the files to the new location.
- mv filename new_file_name : Renames the file to a new filename.
- sudo : Allows regular users to run programs as superuser or root.
- rm
- mkdir
- mkdir
- rmdir
- mv
- man
- history : Gives a list of all past commands typed in the current terminal session.
- clear : Clears the terminal.
---------------------------------------------------------------------
`` pr Command : Printing on Linux
` Pr Command Options
- pr -x : Divides the data into 'x' columns
- pr -h "header" : Assigns "header" value as the report header
- pr -t : Does not print the header and top/bottom margins
- pr -d : Double spaces the output file
- pr -n : Denotes all line with numbers
- pr -l page length : Defines the lines (page length) in a page. Default is 56
- pr -o margin : Formats the page in accordance with the margin number
`` lp Filename or lpr Filename : Hard Copy
- Printing 'N' copies of file
- lp -nN Filename or lpr n Filename
- lp -dPrintername Filename or lpr -PPrintername Filename : Choosing Printers
---------------------------------------------------------------------
`` apt-get : Command used to install and update packages
- sudo apt-get install Softwarename : Installing Software
---------------------------------------------------------------------
`` Mailx address body : Command to send email
`` email address body : Command to send email
---------------------------------------------------------------------
`` Redirection in Linux
` Input / Output
- Standard Input (stdin) device is the keyboard.
- Standard Output (stdout) device is the Screen.
` Output Redirection
- The '>' symbol is used for output (stdout) redirection.
- Output Redirection >
- ">" is the output redirection operator. ">>" appends output to an existing file.
` Input redirection
- The '<' symbol is used for input (stdin) redirection.
- < Input Redirection
- ex) mail -s "News Today" abc@mail.com < NewsFlash
` Error Redirection
- Standard Input STDIN : FD0
- Standard Output STDOUT : FD1
- Standard Error STDERR : FD2
` Why Error Redirection?
- Searching for files typically gets permission denied errors.
- Error messages clutter up program output while executing shell scripts.
- Solution = Redirect error messages
` ls Documents ABC > dirlist 2>&1
- ">&" which writes the output from one file to the input of another file.
- Error output is redirected to standard output which in turn is being re-directed to file dirlist.
- ">&" re-directs output of one file to another.
---------------------------------------------------------------------
`` Pipes
- The symbol '|' denotes a pipe.
- Use Pipes to run two commands consecutively.
- Helps in creating powerful commands
ex) cat aaa.txt | less
` 'pg' and 'more' commands
- cat Filename | pg
- cat Filename | more
`` grep
- Scan a document
- Present the result in a format you want
- grep
ex) cat aaa.txt | grep han
` The 'grep' command
- -v : Shows all the lines that do not match the searched string.
- -c : Displays only the count of matching lines.
- -n : Shows the matching line and its number.
- -i : Match both (upper and lower) case, all lines that match the character.
- -l : Shows just the name of the file with the string.
`` The 'sort' command
- Sorting the contents of a file
- sort Filename
` The 'sort' Option
- -r : Reverses sorting
- -n : Sorts numerically
- -f : Case insensitive sorting
` Pipes '|' help combine 2 or more commands.
` A filter in a pipe is an output of one command which serves as input to the next.
` The grep command can be used to find strings and values in a text document.
` 'sort' command sorts out the content of a file alphabetically.
` less, pg and more commands are used for dividing a long file into readable bits.
---------------------------------------------------------------------
`` Regular Expressions
` tr, sed, vi, grep
- . : replaces any character
- ^ : matches start of string
- $ : matches end of string
- * : matches up zero or more times the preceding character
-\ : Represent special characters
- () : Groups regular expressions
- ? : Matches up exactly one character
` Interval Regular Expressions
- Number of occurrences of a character in a string.
- {n} : Matches the preceding character appearing 'n' times exactly
- {n,m} : Matches the preceding character appearing 'n' times but not more than m
- {n,} : Matches the preceding character only when it appears 'n' times or more
` Extended regular Expressions
- \+ : Matches one or more occurrence of the previous character
- \? : Matches zero or one occurrence of the previous character
` Brace Expansion
- Sequence : {0..10}, {a..z}
- Comma Separated List : {aa, bb, cc, dd}
` Summary
- Regular expressions are a set of characters used to check patterns in strings.
- They are also called 'regexp' and 'regex'.
- It is important to learn regular expressions for writing scripts.
` Some basic regular expressions are:
- . : replaces any character
- ^ : matches start of string
- $ : matches end of string
` Some extended regular expressions are :
- \+ : Matches one or more occurrence of the previous character
- \? : Matches zero or one occurrence of the previous character
` Some interval regular expressions are :
- {n} : Matches the preceding character appearing 'n' times exactly
- {n,m} : Matches the preceding character appearing 'n' times but not more than m
- {n, } : Matches the preceding character only when it appears 'n' times or more
- The brace expansion is used to generate strings. It helps in creating multiple strings out of one.
---------------------------------------------------------------------
`` Managing Processes
` Process?
- An instance of a program is called a Process.
- Any command that you give to your Linux machine starts a new process.
` Types of Processes
- Foreground Processes
- Background Processes
` Background Process
1. Start the program
2. Press Ctrl + Z
3. Type bg to send process to background
` FG Command
- fg (jobname)
` Top
- Displays all the running processes.
- top
` process Status
- D : Uninterruptible sleep
- R : Running
- S : Sleeping
- T : Traced or Stopped
- Z : Zombie
` PS
- Process Status
- ps ux
- ps PID
` kill
- Terminates running processes
- kill PID
- niceness : -20 to 19 : Lower the niceness index, higher would be the priority
` NICE
- Default value of all the processes is 0
- nice -n 'Nice value' process name
- renice 'nice value' -p 'PID'
` DF
- Reports the free disk space
- df
` FREE
- Shows free and used memory (RAM) on the Linux system
- free -m
- free -g
`` Summary
- Any running program or a command given to a Linux system is called a process.
- A process could run in foreground or background.
- The priority index of a process is called Nice in Linux. Its default value is 0 and it can vary between 20 to -19.
- The lower the Niceness index the higher would be priority given to that task.
- bg : To send a process to background
- fg : To run a stopped process in foreground
- top : Details on all Active Processes
- ps : Give the status of processes running for a user
- ps PID : Gives the status of a particular process
- pidof
- kill PID : kills a process
- nice : Starts a process with a given priority
- renice : Changes priority of an already running process
- df : Gives free hard disk space on your system
- free : Tells the free RAM on your system