250 likes | 341 Views
Learn about special characters, basic utilities like ls, cat, cp, mv, and more. Understand file manipulation, sorting, searching, comparing, and using pipes effectively. Discover techniques to work with control characters and handle file names efficiently. Enhance your Linux/Unix skills with practical examples and best practices.
E N D
Lecture 3: Utilities (ch 3) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Outline • Special characters in Linux/Unix • Basic utilities • Display / copy / delete files • Sort / search in files • Compare files, obtain file information • Pipe
Special Characters • List on Pg50 • Avoid unnecessary use • Treat them differently
Special Characters • Examples • echo hello world > hello • cat hello • echo hello * world > hello • cat hello • echo hello ~ world > hello • cat hello
Special Characters • Use quote “ ” or ‘ ’ • echo “hello * world” > hello • echo hello “**” world > hello • echo ‘hello ~ world’ > hello • echo hello ‘~~’ world > hello
Special Characters • Use \ • echo hello \* world > hello • echo hello \*\* world > hello • Invisible/control characters • CTRL-C, CTRL-U, … • Use CTRL-V
Special Characters • Special characters in file names • Create a file “hello world” • echo HELLO WORLD > hello world • ls • cat hello • echo HELLO WORLD > “hello world” • echo HELLO WORLD > hello\ world
Special Characters • List on Pg50 • Avoid unnecessary use • Treat them differently • The safe way is to use “ ” or ‘ ’
Basic Utilities • Preparation • cd /home/your username/it244 • pwd(make sure of the working directory) • /home/shengbo/it244/prep
Basic Utilities • List files • ls –l Size File name
Basic Utilities • Display files • cat linux • more linux • less linux
Basic Utilities • Display files • head linux • tail linux • head –1 linux • tail –5 linux
Basic Utilities • Copy files • cp source-filedestination-file • cp linuxlinux.copy • ls • cp /home/shengbo/it244/welcome welcome • ls • more welcome
Basic Utilities • Copy files • Overwriting • cp hello linux • cat linux • “-i” option • cp –i hello linux • cp linux.copylinux(restore file ‘linux’)
Basic Utilities • Rename files ( mv ) • ls • mvlinux.copymylinux • ls • Overwriting • cat mylinux • cat hello • mvmylinux hello • cat hello • “-i” option
Basic Utilities • Delete files ( rm ) • rm hello • Safe way of deleting (“-i” option) • echo “hello world” > hello • rm –i hello
Basic Utilities • Display a file in order ( sort ) • sort linux • sort days • sort –r months • sort days months • Remove duplicate lines • cat dups • uniqdups
Basic Utilities • Compare two files ( diff ) • diff –u colors.1 colors.2 colors.1 colors.2 red green yellow pink purple orange red blue green yellow orange
Basic Utilities • Compare two files ( diff ) • diff –u colors.1 colors.2 • diff –y colors.1 colors.2 • diff –y colors.1 colors.2 –W 30
Basic Utilities • Identify file types ( file ) • file linux • file hello • file /home/shengbo/it244/exetest • file /home/shengbo/it244/umb.jpg
Basic Utilities • Word counter ( wc ) • wclinux • wc –l months • wc –w hello • Try inputting control characters in a file lines words chars
Basic Utilities • Search a string in files • grep “Linux” linux • grep ‘J’ months • grep n months days
Basic Utilities • Pipe (communication between commands) • command1 | command 2 • sort months | head -4 • ls | wc –w • ls | grep “color” • ls /home | more
Summary • Special characters • Basic utilities • ls, cat, more, less, head, tail • cp, mv, rm(overwrite, ‘-i’) • sort, uniq, diff, file, wc, grep • | (pipe)
Questions • How to display the lines in the middle of a file? For example, in file ‘months’, display from “May” to “Sep”. • uniq can only remove successive duplicate lines. How to remove all the duplicate lines in a file? e.g., display ‘Mary’ only once when operating on file ‘dups’.