美文网首页
bash shell 基础命令

bash shell 基础命令

作者: QXPLUS | 来源:发表于2023-10-17 19:56 被阅读0次
  • 来自Linux命令行与shell脚本编程大全,如有侵权,请联系删除
  1. 启动shell
    /etc/passwd 文件包含了所有系统账户以及每个用户的基本配置信息
$ cat /etc/passwd | grep "root"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

这里 root用户 将/bin/bash作为默认shell程序

  1. 查看文件
    ls -F 列出当前文件夹下的文件和目录, 并区分目录
$ ls
job_example  perl5  proj_doing  proj_done  software  Work
## 发现当前全部是目录
$ ls -F
job_example/  perl5/  proj_doing/  proj_done/  software/  Work/

ls -a 列出全部文件和目录,包括隐藏目录

$ ls -a
.              .bash_logout   .cache    .config      .kshrc    .mkshrc         perl5       proj_done        .ssh      Work
..             .bash_profile  .conda    .emacs       .lesshst  .mozilla        .pki        .python_history  .vim      .Xauthority
.bash_history  .bashrc        .condarc  job_example  .local    .mysql_history  proj_doing  software         .viminfo  .zshrc

$ ls -aF
./             .bash_logout   .cache/   .config/      .kshrc    .mkshrc         perl5/       proj_done/       .ssh/     Work/
../            .bash_profile  .conda/   .emacs        .lesshst  .mozilla/       .pki/        .python_history  .vim/     .Xauthority
.bash_history  .bashrc
  1. 处理文件

touch创建文件一个空文件

$ touch test_one

$ ls -l test_one 
-rw-r--r-- 1 r01 research 0 Oct 18 16:52 test_one

cp复制文件

## 复制文件到当前路径
cp  test_one test_two
### -i 选项 询问是否覆盖
$ cp -i test_one test_two
cp: overwrite ‘test_two’? y
## 复制文件到指定路径
$ mkdir Docu
$ cp test* Docu/
$ ls -l Docu
total 0
-rw-r--r-- 1 r01 research 0 Oct 18 16:57 test_one
-rw-r--r-- 1 r01 research 0 Oct 18 16:57 test_two

ln -s 创建软连接(符号链接)
软连接是一个 文件
链接文件,是Linux文件系统的优势。如果需要在系统中维护同一文件的两个或者多个副本,可以使用 单个物理副本 加 多个虚拟副本(链接) 代替创建多个物理副本。
链接 是目录中 指向文件真实位置的占位符

$ ln -s test_one slink_test_one

$ ls -l *test_one
lrwxrwxrwx 1 r01 research 8 Oct 18 18:50 slink_test_one -> test_one
-rw-r--r-- 1 r01 research 0 Oct 18 16:52 test_one
## 查看两个文件的inode编号
$ ls -i *test_one
71646507789 slink_test_one  79173466884 test_one

ln 创建 硬链接
硬链接是一个 虚拟文件

ln test_two slink_test_two
## 硬链接是一个独立的 虚拟文件,包含原始文件的信息及其位置。
## 本质上,与原始文件是同一个文件
$ ls -il *test_two
79174740228 -rw-r--r-- 2 r01 research 0 Oct 18 16:55 slink_test_two
79174740228 -rw-r--r-- 2 r01 research 0 Oct 18 16:55 test_two

mv 文件重命名
只影响文件名, 重命名后的 inode 编号 和 时间戳不变

ll -i test_one 
79173466884 -rw-r--r-- 1 r01 research 0 Oct 18 16:52 test_one

$ mv test_one test_one2

$ ll -i test_one2
79173466884 -rw-r--r-- 1 r01 research 0 Oct 18 16:52 test_one2

rm 删除文件
注意:shell 没有回收站或者垃圾箱之类的东西, 文件一旦被删除,就找不回来了。

## 建议加入 -i 选项 ,删除是进行再次确认
$ rm -i slink_test_one
rm: remove symbolic link ‘slink_test_one’? y
  1. 管理目录

mkdir 创建目录

$ mkdir New_Dir
# -d 选项 显示当前文件夹
$ ls -ld New_Dir
drwxr-xr-x 2 r01 research 4096 Oct 18 19:37 New_Dir

mkdir -p 循环创建目录及其子目录

$ mkdir -p New_Dir/SubDir/UnderDir
## -R 选项 查看目录下的所有文件、及其子目录下的所有文件
$ ls -R New_Dir
New_Dir:
SubDir

New_Dir/SubDir:
UnderDir

New_Dir/SubDir/UnderDir:

rmdir 删除 空目录

$ mkdir wrongDir
$ ll -d wrong*
drwxr-xr-x 2 r01 research 4096 Oct 18 19:45 wrongDir

$ rmdir wrongDir
$ 

## 非空目录无法删除
$ mkdir wrongDir2
$ touch wrongDir2/file1
$ ll wrongDir2/
total 0
-rw-r--r-- 1 r01 research 0 Oct 18 19:47 file1

$ rmdir wrongDir2
rmdir: failed to remove ‘wrongDir2’: Directory not empty

rm -r 删除非空目录

$ rm -ri wrongDir2
rm: descend into directory ‘wrongDir2’? y
rm: remove regular empty file ‘wrongDir2/file1’? y
rm: remove directory ‘wrongDir2’? y
$ 

rm -rf 会一口气删完所有文件,不会有任何提示,建议谨慎使用。

  1. 查看文件内容

file 命令,是一个很方便的小工具,能够探测文件的内部并判断文件类型。

## .bashrc  是一个text文件,字符编码为ASCII
$ file .bashrc
.bashrc: ASCII text
## Work 是一个文件夹
$ file Work
Work: directory
## slink_two  是 test_two 的符号连接(软链接)
$ file slink_two 
slink_two: symbolic link to 'test_two'
## test_two 文件是一个shell 脚本
$ file test_two 
test_two: Bourne-Again shell script, ASCII text executable

$ which ls
alias ls='ls --color=auto'
    /usr/bin/ls
$ file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ceaf496f3aec08afced234f4f36330d3d13a657b, stripped

cat 查看文件

$ cat test_two 
#!/bin/bash

echo "Hello world"
$
$ cat -n test_two 
     1  #!/bin/bash
     2  
     3  echo "Hello world"
$
$ cat -b test_two 
     1  #!/bin/bash

     2  echo "Hello world"
$

more 分页显示文本文件内容

$ more /etc/profile

profile 文件显示如下

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
--More--(35%)

less more的升级版本。(less is more)

$ less /etc/profile

打开profile文件如下

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
/etc/profile

head
tail
查看部分文件
head -n tail -n 查看指定的行

相关文章

  • shell & bash基础命令及巧用

    shell与bash脚本的区别shell是Linux基础命令解释器bash(Bourne Again shell)...

  • shell编程-bash shell的数值计算

    1. 数据计算基础 使用man 或者info查看shell bash的判断命令test,Shell中的 test ...

  • 初识shell脚本编程

    一. Shell基础 shell是链接用户和linux内核的一个命令解释程序, 常见shell包括bash(li...

  • chapter 3.基本的bash shell命令

    基本的bash shell命令 启动shell 大多数Linux默认的shell都为GNU bash shell/...

  • Day-2初识linux

    1.bash shell 是什么? bash shell 是一个命令解释器,用户输入命令之后,通过bash she...

  • linux基础命令

    命令行基础 一些名词 「图形界面」 「命令行」 「终端」 「shell」 「bash」 安装使用 Windws: ...

  • shell 编程学习

    当前shell执行命令。./或者source 新建shell:/bin/bash ./file.sh bash $...

  • bash的初识。。。008

    bash的基础特性:命令历史:shell进程会保存在其会话中执行过的命令 history : 查看过往历史命令 ...

  • git命令行基础

    命令行基础 图像界面 图形界面 命令行 终端 shell bash 安装使用 windows:安装git,打开gi...

  • day02-Linux学习 Bash基础

    一、Bash Shell初步认识 1.什么是Bash Shell(壳)? 命令解释器, 将用户输入的命令,翻译给内...

网友评论

      本文标题:bash shell 基础命令

      本文链接:https://www.haomeiwen.com/subject/ryqqidtx.html