源自:https://linuxjourney.com/
个人学习
1.The Shell
bash shell(bourne again shell) default almost linux
other shells: ksh, zsh, tsch
format:
username@hostname:current_directory
pete@icebox:/home/pete $
notice: $ is for a normal user using Bash,Bourne or Korn shell .
# is for root user
a simple command. echo.-- print out the text arguments to the display
$ ehco Hello World
result: Hello World
2.pwd(Print Working Directory)
Everything in linux is a file.Every file is organized in a hierachical directory tree.
The first directory in the filesystem is aptly named the root directory.
Here is an example of what the directory tree looks like:
/
| - - bin
| | - - file1
| | - - file2
| - - etc
| | - - file3
| ’ - - directory1
| | - - file4
| ’ - - file5
| - - home
| - - var
To see where you are,you can use the pwd command,it’s means “print working directory” and just shows you which directory you are in,note the path stems from the root directory.
$ pwd
3.cd (Change Directory)
There are two different ways to specify a path,with absolute and ralative paths.
- absolute path:This is the path from the root directory.The root directory is commonly shown as a slash.Every time your path starts with / it means you are starting from the directory.For example,/home/pete/Desktop.
- Relative path: This is the path where you are currently in filesystem.If I was in location /home/pete/Documents and wanted to get to a directory insaide Documents callled taxes, I don/t hace to specify the whole path from root like /home/pete/Documents/taxes, I can just go to taxes/ insead.
$ cd /home/pete/Pictures
So now I’ve changed my directory location to /home/pete/Pictures.
It can get pretty tiring navigating with absolute and ralative paths all the time,luckily there are some shortcuts to help you out.
- .(current directory). This is the directory you are currently in.
- …(parent directory). Takes you to the directory above you current.
- ~(home directory).This directory dafaults to your “home directory”.Such as /home/pete.
- -(previous directory).This will take you to the previous directory you were just at.
$ cd .
$ cd …
$ cd ~
$ cd -
4.ls (List Directories)
The ls command will list directories and files in the current directory by default,however you can specify which path you want to list the directories of.
$ ls
$ ls /home/pete
a for all
$ ls -a
there is also one more useful ls flag , -l for long,this shows a detailed list of files in a long fornat.
This will show you detailed information,starting from the left:file permissions, number of links, ower name. owner group, file size, timestamp of last modification, and file/directory name.
$ ls -l
Commands have things called flags( or arguments or options, whatever you want to call it) to add more functionality.
ls -R: recursively list directory contens
ls -r : reverse order while sorting
ls -t: sort by modification time,newest first
5.touch
Let’s learn how to make some files.A very simply way is to use the touch command.Touch allows you to the create new empty files.
$ touch mysuperduperfile
Touch is also to used to change timestamps on existing files and directories.Give it a try,do an ls -l on a file and note the timestamp,then touch that file and it will update the timestamp.
6.file
Did you notice that the filename didn’t conform to standard naming like you’ve probably seen with other operating systems like Windows? Normally you would expect a file called banana.jpeg and expect a JPEG picture file.
In Linux, filenames aren’t required to represent the contents of the file. You can create a file called funny.gif that isn’t actually a GIF.
To find out what kind of file a file is, you can use the file command. It will show you a description of the file’s contents.
$ file banana.jpg
7.cat
We’re almost done navigating files,but first let’s learn how to read a file.A simple command to use is the cat command,short for concatenate,it not only displays file contents but can combine mutiple file and show you the output of them.
$ cat dogfile birdfile
8.less
The text is displayed in a paged manner, so you can navigate through a text file page by page.
Once you’re in the less command,you can actually use other keyboard commands to navigate in the file.
$ less /home/pete/Documents/text1
Use the following command to navigate through less:
q - Used to quit out of less and go back to your shell.
Page up,Page Down , and Down - Navegate using the arrow keys and page keys.
g - moves to beginning of the text file
G - moves to the end of the text file
/search - you can search of specific text inside the text document.Prefacing the words you want to search with /
h - if you need a little help zbout how to use less while in less,use help
9.history
This is quite useful when you want to find and run a command you used previously without actually typing it again.
$ history
Want to run the same command you did before, just hit the up arrow.
Want to run the previous command without typing it again? Use !!. If you typed cat file1 and want to run it again, you can actually just go !! and it will run the last command you ran.
Our terminal is getting a little cluttered no? Let’s do a little cleanup, use the clear command to clear up your display.
$ clear
While we are talking about useful things, one of the most useful features in any command-line environment is tab completion. If you start typing the beginning of a command, file, directory, etc and hit the Tab key, it will autocomplete based on what it finds in the directory you are searching as long as you don’t have any other files that start with those letters.
10.cp(Copy)
$ cp mycoolfile /home/pete/Documents/cooldocs
mycoolfile is the file you want to copy and /home/pete/Documents/cooldocs is where you are copying the file to.
- the wildcard of wildcards, it’s used to represent all single characters or any string.
? used to represent one character
[] used to represent any character within the brackets
$ cp *.jpg /home/pete/Pictures
This will copy all files with the .jpg extension in your current directory to the Pictures directory.
A useful command is to use the -r flag, this will recursively copy the files and directories within a directory
$ cp -r Pumpkin/ /home/pete/Documents
You can use the -i flag (interactive) to prompt you before overwriting a file.
$ cp -i mycoolfile /home/pete/Pictures
11.mv(Move)
rename
$ mv oldfile newfile
move a file to a different directory:
$mv file2 /home/pete/Documents
move more than one file:
$ mv file_1 file_2 /somedirectory
rename directories:
$ mv directory1 directory2
Like cp,mv file or directory it will overwrite anything in the same directory.So you can use the -i flag to prompt you before overwriting anything.
mv -i directory1 directory2
You can also make a backup of that file and it will just rename the old version with a ~.
$ mv -b directory1 directory2
12.mkdir(Make Directory)
create a new directory or make multiple directories at the same time
$ mkdir books paintings
create subdirectories at the same time with the -p (parent flag)
$ mkdir -p books/hemmingway/favorites
13.rm(Remove)
The rm(Remove) command is used to delete files and directories.
$ rm file1
不可逆转的删除!
$ rm -f file1
不用经过允许,强制性删除
$ rm -i file
请求允许
$ rm -r directory
删除目录(默认是不能删除的,必须加上-r)
$ rmdir directory
删除目录
14.find
$ find /home -name puppies.jpg
$find /home -type d -name MyFolder
15.help
$ help echo
This will give you a description and the options you can use when you want to run echo. For other executable programs, it’s convention to have an option called --help or something similar.
$ echo - - help
16.man
$ man ls
Man pages are manuals that are by default built into most Linux operating systems. They provide documentation about commands and other aspects of the system.
Try it out on a few commands to get more information about them.
17.whatis
$ whatis cat
if you are ever feeling doubtful about what a command does, you can use the whatis command. The whatis command provides a brief description of command line programs.
18.alias
To create an alias for a command you simply specify an alias name and set it to the command.
$ alias foobar=‘ls -la’
Keep in mind that this command won’t save your alias after reboot, so you’ll need to add a permanent alias in:
~/.bashrc
You can remove aliases with the unalias command:
$ unalias foobar
19.exit
To exit from the shell, you can use the exit command
$ exit
Or the logout command:
$ logout
总结:
十九个基本命令,
$ echo Hello World 直接输出Hello World
$ pwd 打印当前工作目录
$ cd 改变目录
$ ls 显示目录文件
$ touch 新建文件
$ file 查看文件或者目录的类型
$ cat 对内容较少的文件直接查看
$ less 一页一页查看文件内容
$ history 使用过的命令记录
$ cp 赋值粘贴文件
$ mv 移动或者重命名文件,目录
$ mkdir 新建目录
$ rm 删除文件 rmdir 删除目录
$ find 查找文件或目录
$ help 命令提示
$ man 命令文档
$ whatis 命令文档
$ alias 对比较长的命令包装易于使用
$ exit 退出 login out 注销