The Linux Command Line---Looking Around

The Linux Command Line by William Shotts

Looking Around

Now that you know how to move from working directory to working directory, we're going to take a tour of your Linux system and, along the way, learn some things about what makes it tick. But before we begin, I have to teach you some tools that will come in handy during our adventure. These are:
  • ls (list files and directories)
  • less (view text files)
  • file (classify a file's contents)

ls

The ls command is used to list the contents of a directory. It is probably the most commonly used Linux command. It can be used in a number of different ways. Here are some examples:
Examples of the ls command
CommandResult
lsList the files in the working directory
ls /binList the files in the /bin directory (or any other directory you care to specify)
ls -lList the files in the working directory in long format
ls -l /etc /binList the files in the /bin directory and the /etc directory in long format
ls -la ..List all files (even ones with names beginning with a period character, which are normally hidden) in the parent of the working directory in long format
These examples also point out an important concept about commands. Most commands operate like this:
    command -options arguments
where command is the name of the command, -options is one or more adjustments to the command's behavior, and arguments is one or more "things" upon which the command operates.
In the case of ls, we see that ls is the name of the command, and that it can have one or more options, such as -a and -l, and it can operate on one or more files or directories.

A Closer Look At Long Format

If you use the -l option with ls, you will get a file listing that contains a wealth of information about the files being listed. Here's an example:


-rw-------   1 bshotts  bshotts       576 Apr 17  1998 weather.txt
drwxr-xr-x   6 bshotts  bshotts      1024 Oct  9  1999 web_page
-rw-rw-r--   1 bshotts  bshotts    276480 Feb 11 20:41 web_site.tar
-rw-------   1 bshotts  bshotts      5743 Dec 16  1998 xmas_file.txt

----------     -------  -------  -------- ------------ -------------
    |             |        |         |         |             |
    |             |        |         |         |         File Name
    |             |        |         |         |
    |             |        |         |         +---  Modification Time
    |             |        |         |
    |             |        |         +-------------   Size (in bytes)
    |             |        |
    |             |        +-----------------------        Group
    |             |
    |             +--------------------------------        Owner
    |
    +----------------------------------------------   File Permissions


File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification occurred more than six months in the past, the date and year are displayed. Otherwise, the time of day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition to the file's owner.
Owner
The name of the user who owns the file.
File Permissions
A representation of the file's access permissions. The first character is the type of file. A "-" indicates a regular (ordinary) file. A "d" indicates a directory. The second set of three characters represent the read, write, and execution rights of the file's owner. The next three represent the rights of the file's group, and the final three represent the rights granted to everybody else. I'll discuss this in more detail in a later lesson.

less

less is a program that lets you view text files. This is very handy since many of the files used to control and configure Linux are human readable.
The less program is invoked by simply typing:
less text_file
This will display the file.

Controlling less

Once started, less will display the text file one page at a time. You may use the Page Up and Page Down keys to move through the text file. To exit less, type "q". Here are some commands that less will accept:
Keyboard commands for the less program
CommandAction
Page Up or bScroll back one page
Page Down or spaceScroll forward one page
GGo to the end of the text file
1GGo to the beginning of the text file
/charactersSearch forward in the text file for an occurrence of the specified characters
nRepeat the previous search
hDisplay a complete list less commands and options
qQuit

file

As you wander around your Linux system, it is helpful to determine what kind of data a file contains before you try to view it. This is where the file command comes in. file will examine a file and tell you what kind of file it is.
To use the file program, just type:
file name_of_file

The file program can recognize most types of files, such as:
Various kinds of files
File TypeDescriptionViewable as text?
ASCII textThe name says it allyes
Bourne-Again shell script textbash scriptyes
ELF 32-bit LSB core fileA core dump file (a program will create this when it crashes)no
ELF 32-bit LSB executableAn executable binary programno
ELF 32-bit LSB shared objectA shared libraryno
GNU tar archiveA tape archive file. A common way of storing groups of files.no, use tar tvf to view listing.
gzip compressed dataAn archive compressed with gzipno
HTML document textA web pageyes
JPEG image dataA compressed JPEG imageno
PostScript document textA PostScript fileyes
RPMA Red Hat Package Manager archiveno, use rpm -q to examine contents.
Zip archive dataAn archive compressed with zipno
While it may seem that most files cannot be viewed as text, you will be surprised how many can. This is especially true of the important configuration files. You will also notice during our adventure that many features of the operating system are controlled by shell scripts. In Linux, there are no secrets!

The Linux Command Line---Navigation

The Linux Command Line by William Shotts

Navigation

In this lesson, I will introduce your first three commands: pwd (print working directory), cd (change directory), and ls (list files and directories).
If you have not worked with a command line interface before, you will need to pay close attention to this lesson, since the concepts will take some getting used to.

File System Organization

Like that legacy operating system, the files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.

Most graphical environments today include a file manager program to view and manipulate the contents of the file system. Often you will see the file system represented like this:
One important difference between the legacy operating system and Unix-like operating systems such as Linux is that Linux does not employ the concept of drive letters. While drive letters split the file system into a series of different trees (one for each drive), Linux always has a single tree. Different storage devices may contain different branches of the tree, but there is always a single tree.

pwd

Since a command line interface cannot provide graphic pictures of the file system structure, it must have a different way of representing it. Think of the file system tree as a maze, and you are standing in it. At any given moment, you are located in a single directory. Inside that directory, you can see its files and the pathway to its parent directory and the pathways to the subdirectories of the directory in which you are standing.
The directory you are standing in is called the working directory. To find the name of the working directory, use the pwd command.
[me@linuxbox me]$ pwd
/home/me
When you first log on to a Linux system, the working directory is set to your home directory. This is where you put your files. On most systems, your home directory will be called /home/your_user_name, but it can be anything according to the whims of the system administrator.
To list the files in the working directory, use the ls command.
[me@linuxbox me]$ ls
Desktop     Xrootenv.0    linuxcmd
GNUstep     bin           nedit.rpm
GUILG00.GZ  hitni123.jpg  nsmail
I will come back to ls in the next lesson. There are a lot of fun things you can do with it, but I have to talk about pathnames and directories a bit first.

cd

To change your working directory (where you are standing in the maze) you use the cd command. To do this, type cd followed by the pathname of the desired working directory. A pathname is the route you take along the branches of the tree to get to the directory you want. Pathnames can be specified in one of two different ways; absolute pathnames or relative pathnames. Let's look with absolute pathnames first.
An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. For example, there is a directory on your system in which most programs are installed. The pathname of the directory is /usr/bin. This means from the root directory (represented by the leading slash in the pathname) there is a directory called "usr" which contains a directory called "bin".
Let's try this out:
[me@linuxbox me]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin
[me@linuxbox bin]$ ls
[                     lwp-request
2to3                  lwp-rget
2to3-2.6              lxterm
a2p                   lz
aalib-config          lzcat
aconnect              lzma
acpi_fakekey          lzmadec
acpi_listen           lzmainfo
add-apt-repository    m17n-db
addpart               magnifier

and many more...
Now we can see that we have changed the current working directory to /usr/bin and that it is full of files. Notice how your prompt has changed? As a convenience, it is usually set up to display the name of the working directory.
Where an absolute pathname starts from the root directory and leads to its destination, a relative pathname starts from the working directory. To do this, it uses a couple of special notations to represent relative positions in the file system tree. These special notations are "." (dot) and ".." (dot dot).
The "." notation refers to the working directory itself and the ".." notation refers to the working directory's parent directory. Here is how it works. Let's change the working directory to /usr/bin again:
[me@linuxbox me]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin
O.K., now let's say that we wanted to change the working directory to the parent of /usr/bin which is /usr. We could do that two different ways. First, with an absolute pathname:
[me@linuxbox bin]$ cd /usr
[me@linuxbox usr]$ pwd
/usr
Or, with a relative pathname:
[me@linuxbox bin]$ cd ..
[me@linuxbox usr]$ pwd
/usr
Two different methods with identical results. Which one should you use? The one that requires the least typing!
Likewise, we can change the working directory from /usr to /usr/bin in two different ways. First using an absolute pathname:
[me@linuxbox usr]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin
Or, with a relative pathname:
[me@linuxbox usr]$ cd ./bin
[me@linuxbox bin]$ pwd
/usr/bin
Now, there is something important that I must point out here. In almost all cases, you can omit the "./". It is implied. Typing:
[me@linuxbox usr]$ cd bin
would do the same thing. In general, if you do not specify a pathname to something, the working directory will be assumed. There is one important exception to this, but we won't get to that for a while.

A Few Shortcuts

If you type cd followed by nothing, cd will change the working directory to your home directory.
A related shortcut is to type cd ~user_name. In this case, cd will change the working directory to the home directory of the specified user.
Typing cd - changes the working directory to the previous one.

The Linux Command Line---What Is "The Shell"?



The Linux Command Line by William Shotts


What Is "The Shell"?

Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.
On most Linux systems a program called bash (which stands for Bourne Again SHell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne) acts as the shell program. Besides bash, there are other shell programs that can be installed in a Linux system. These include: kshtcsh and zsh.

What's A "Terminal?"

It's a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: gnome-terminalkonsolextermrxvtkvtnxterm, and eterm.

Starting A Terminal

Your window manager probably has a way to launch a terminal from the menu. Look through the list of programs to see if anything looks like a terminal emulator. If you are a KDE user, the terminal program is called "konsole," in Gnome it's called "gnome-terminal." You can start up as many of these as you want and play with them. While there are a number of different terminal emulators, they all do the same thing. They give you access to a shell session. You will probably develop a preference for one, based on the different bells and whistles each one provides.

Testing The Keyboard

OK, let's try some typing. Bring up a terminal window. You should see a shell prompt that contains your user name and the name of the machine followed by a dollar sign. Something like this:
[me@linuxbox me]$
Excellent! Now type some nonsense characters and press the enter key.
[me@linuxbox me]$ kdkjflajfks
If all went well, you should have gotten an error message complaining that it cannot understand you:
[me@linuxbox me]$ kdkjflajfks
bash: kdkjflajfks: command not found
Wonderful! Now press the up-arrow key. Watch how our previous command "kdkjflajfks" returns. Yes, we have command history. Press the down-arrow and we get the blank line again.
Recall the "kdkjflajfks" command using the up-arrow key if needed. Now, try the left and right-arrow keys. You can position the text cursor anywhere in the command line. This allows you to easily correct mistakes.

You're not logged in as root, are you?

If the last character of your shell prompt is # rather than $, you are operating as the superuser. This means that you have administrative privileges. This can be potentially dangerous, since you are able to delete or overwrite any file on the system. Unless you absolutely need administrative privileges, do not operate as the superuser.

Using The Mouse

Even though the shell is a command line interface, the mouse is still handy.
Besides using the mouse to scroll the contents of the terminal window, you can copy text with the mouse. Drag your mouse over some text (for example, "kdkjflajfks" right here on the browser window) while holding down the left button. The text should highlight. Release the left button and move your mouse pointer to the terminal window and press the middle mouse button (alternately, you can press both the left and right buttons at the same time if you are working on a touch pad). The text you highlighted in the browser window should be copied into the command line.

更改Windows远程端口3389


为了防止被暴力破解,建议修改Windows的远程3389端口

修改方法如下:

WIN + R 打开运行窗口,输入regedit打开注册表

依次展开

1、

"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp"

将右侧"PortNumber"键值做修改,切换十进制,改为你想要的端口

2、

"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"

原理同上修改"PortNumber"键值

3、

将修改后的端口加入防火墙通行规则中,重启生效

4、

为了提高安全,可以将超级管理员Administrator的名字进行重命名

以防止字典暴力破解

修改路径为-右键电脑-管理-本地用户和组-用户-对Administrator重命名

Virtual Box 通过U盘启动


把U盘挂载到Virtual Box上做硬盘,也可测试U盘启动

1、以管理员Administrator身份运行Virtual Box

2、将U盘插入电脑的USB接口

3、以管理员Administrator身份运行cmd.exe

4、通过cmd进入到Virtual Box安装目录下,比如(C:\Program Files\Oracle\VirtualBox)

示例:  cd  "C:\Program Files\Oracle\VirtualBox"

5、输入以下命令,enter回车运行

VBoxManage internalcommands createrawvmdk -filename "D:\usb.vmdk" -rawdisk \\.\PHYSICALDRIVE1

-------------------------------------------
VBoxManage internalcommands createrawvmdk -filename 无需更改

 "D:\usb.vmdk"  虚拟USB硬盘保存位置及名称,保留此文件,后期插入U盘可以随时调用

-rawdisk  参数无需更改

\\.\PHYSICALDRIVE1    需要更改U盘的磁盘编号

通过电脑管理,或者win+R 运行输入【diskmgmt.msc】 打开磁盘管理器

查看U盘的磁盘编号

若U盘为磁盘1 则\\.\PHYSICALDRIVE1
若U盘为磁盘2 则\\.\PHYSICALDRIVE2
-------------------------------------------

6、将usb.vmdk添加到虚拟机的磁盘中,启动!

关闭Windows 10 自动更新 turn off windows 10 update


组合键 “win”+ "R" 打开运行窗口,输入 services.msc 确定,打开服务控制台

或者

右键“此电脑”管理,左侧打开“服务和应用程序”选择服务

或者

Windows桌面左下角的搜索,搜索“服务”或者“services.msc” 打开服务控制台

在右侧列表中找到 Windows  Update  双击,或者右键-属性

打开窗口中,将启动类型改为“禁用”

顶端菜单切换到恢复页面,将第一次失败,第二次失败,后续失败,都改为“无操作”

确定,关闭界面即可

界面如下:



Windows10下获取同一文件夹下的文件名或路径


在文件夹中,有若干个文件,现在要求把这些文件的名称提取到txt文本文档或excel中

方法1:Windows 10 自带功能

选中所有需要提取名称的文件,按住shift,右键点击选中文件,选择【复制为路径】

或者选中文件后组合键 【shift + F10】,选择【复制为路径】

即可将结果粘贴至txt 或excel 中,结果会显示该文件的完整路径

方法2:tree命令

tree /f  >  tree.txt

将文件夹下的文件递归显示目录树保存到tree.txt中

个人感觉方法1更好一些

还可以用dir or  ls > tree.txt


Linux自定义命令


在Linux下操作通常都是通过命令来完成的

有的常用操作需要一长串命令来完成,比如通过ssh访问其它的Linux,每次都需要输入

在Linux中可以通过自定义命令来简化这个过程

比如sshvps1可以实现 ssh -p 22 [email protected]这样的操作

方法如下:

编辑用户目录下的 .bashrc 文件 可以通过 ls -al 命令来查看该隐藏文件

在末尾加上 alias sshvps1='ssh -p 22 [email protected]'  保存退出
注意:=号前后不要有空格

重新加载配置文件source .bashrc 使其生效,或者下次登录时也可以生效

在Windows下通过子系统来管理Linux机器最实用,一条命令即可访问远端Linux

也可以一条命令来传输两端的文件,比如scp

windows下Linux子系统安装参见windows 10 安装Linux子系统

文件传输参见使用scp命令在两台机器间传输数据



windows 10 安装Linux子系统


Windows 10 内置了Linux子系统,目前支持多个发行版

如:Ubuntu,openSUSE Leap 42,SUSE Linux enterprise server 12
Debian,Kali Linux

启用方式如下:


1、右键开始菜单打开Windows PowerShell(管理员)


输入以下命令

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

自动开始安装Linux子系统,完成后会提示重启电脑

注:以上适用 Windows bulid 16215 或者之后的版本,早于此前的版本,请使用lxrun命令来自行安装



2、打开Microsoft store,搜索wsl(全称Windows Subsystem for Linux



选择你想安装的版本获取安装即可


说明:如系统内没有Microsoft store,或者诸如下面的问题

There are several scenarios in which you may not be able (or want) to, install WSL Linux distros via the Microsoft Store. Specifically, you may be running a Windows Server or Long-Term Servicing (LTSB/LTSC) desktop OS SKU that doesn't support Microsoft Store, or your corporate network policies and/or admins to not permit Microsoft Store usage in your environment.
可能你需要以下方式来安装
Manually download WSL distro packages





Visual Studio Code 一款易用的代码编辑器



引用Wiki的话

Visual Studio Code(简称VS Code)是一个由微软开发的,同时支持Windows、Linux和macOS操作系统且开放源代码的文本编辑器,它支持调试,并内置了Git 版本控制功能,同时也具有开发环境功能,例如代码补全、代码片段、代码重构等,该编辑器支持用户自定义配置,例如改变主题颜色、键盘快捷方式、编辑器属性和其他参数,还支持扩展程序并在编辑器中内置了扩展程序管理的功能


官网主页:Visual Studio Code

Visual Studio Code可以说是一个全能的文本编辑器,类似的还有sublime text,Emacs等

界面如下:



可以安装扩展程序对编辑器进行升级

快捷键有很多,也可以自己自定义,具体的可以自己查看下

打开程序后可以通过快捷键【Ctrl + K,Ctrl + S】打开所有快捷键的列表

先按Ctrl + K  然后再按 Ctrl + S,这样就调出了快捷键的界面

另,通过快捷键 Ctrl + Shift + P 可以打开所有命令的查找界面,支持模糊查找





使用scp命令在两台机器间传输数据


有时候需要将一台机器中的文件或者文件夹传输到另一台机器中

如果文件在网站目录下,可以直接下载过来

但是非网站目录就需要其它工具来传输

这次说的是scp这个命令,用法如下

scp [可选参数] [路径1] [路径2]

参数说明:

-1: 强制scp命令使用协议ssh1
-2: 强制scp命令使用协议ssh2
-4: 强制scp命令只使用IPv4寻址
-6: 强制scp命令只使用IPv6寻址
-B: 使用批处理模式(传输过程中不询问传输口令或短语)
-C: 允许压缩。(将-C标志传递给ssh,从而打开压缩功能)
-p:保留原文件的修改时间,访问时间和访问权限。
-q: 不显示传输进度条。
-r: 递归复制整个目录。
-v:详细显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
-c cipher: 以cipher将数据传输进行加密,这个选项将直接传递给ssh。
-F ssh_config: 指定一个替代的ssh配置文件,此参数直接传递给ssh。
-i  identity_file: 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l  limit: 限定用户所能使用的带宽,以Kbit/s为单位。
-o ssh_option: 如果习惯于使用ssh_config(5)中的参数传递方式,
-P port:注意是大写的P, port是指定数据传输用到的端口号
-S program: 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

示例1:单文件传输
scp [可选参数] [路径1] [路径2]
[路径1] /tmp/book/
[路径2] [email protected]:/home/test

本地传输/tmp/book目录下的【123.txt】文件到另一台机器的/home/test 目录下

scp /tmp/book/123.txt  [email protected]:/home/test

会提示输入密码,验证后开始传输(用户名为目标机器的已有用户)

也可以不添加用户名,在连接时手动输入

scp /tmp/book/123.txt  1.2.3.4:/home/test

如果1.2.3.4更改过ssh端口,则需要在scp后添加参数-P

scp -P 12345 /tmp/book/123.txt  [email protected]:/home/test

示例2:文件夹传输

文件夹传输是在原有参数后再添加-r的参数即可

scp -r /tmp/book/  [email protected]:/home/test

从远端机器传输文件到本地时,只要将上述示例中的两个路径交换位置即可

如:scp  [email protected]:/home/test/123.txt   /tmp/book/