Linux-centos6.5-(第二篇)文件与目录管理

目录

Linux文件结构及功能

文件夹创建

文件夹查看

ll /usr/

ll –a -S /usr/

切换文件夹

删除文件夹

拷贝文件

服务器间拷贝文件

创建文件

查看文件

移动文件或者修改文件名称

查看文件夹路径

搜索文件

搜索文件总内容

重导命令

管道命令


Linux文件结构及功能

ls -a /

-rw-r--r--.   1 root root     0 Mar 31 11:25 .autofsck

dr-xr-xr-x.   2 root root  4096 Dec  2 10:27 bin

dr-xr-xr-x.   5 root root  1024 Nov 29 21:42 boot

drwx------.   3 root root  4096 Nov 29 21:42 .dbus

drwxr-xr-x.  21 root root  3860 Mar 31 11:25 dev

drwxr-xr-x. 118 root root 12288 Mar 31 12:25 etc

drwx------.   2 root root  4096 Mar 31 12:24 .gnupg

drwxr-xr-x.  11 root root  4096 Mar 31 12:09 home

dr-xr-xr-x.  11 root root  4096 Dec  1 10:59 lib

dr-xr-xr-x.   9 root root 12288 Mar 31 12:25 lib64

drwx------.   2 root root 16384 Nov 29 20:56 lost+found

drwxr-xr-x.   3 root root  4096 Mar 27 20:28 media

drwxr-xr-x.   2 root root     0 Mar 31 11:25 misc

drwxr-xr-x.   2 root root  4096 Sep 23  2011 mnt

drwxr-xr-x.   2 root root     0 Mar 31 11:25 net

drwxr-xr-x.   4 root root  4096 Dec  1 15:44 opt

dr-xr-xr-x. 185 root root     0 Mar 31  2019 proc

drwx------.   2 root root  4096 Nov 29 21:48 .pulse

-rw-------.   1 root root   256 Nov 29 21:48 .pulse-cookie

dr-xr-x---.  39 root root  4096 Mar 31 11:48 root

dr-xr-xr-x.   2 root root 12288 Dec  2 10:27 sbin

drwxr-xr-x.   7 root root     0 Mar 31  2019 selinux

drwxr-xr-x.   2 root root  4096 Sep 23  2011 srv

drwxr-xr-x.  13 root root     0 Mar 31  2019 sys

drwxrwxrwt.  16 root root  4096 Mar 31 12:24 tmp

drwxr-xr-x.  14 root root  4096 Dec  3 21:13 usr

drwxr-xr-x.  22 root root  4096 Nov 29 21:14 var

对这些目录的解释

(http://baijiahao.baidu.com/s?id=1580835752353077411&wfr=spider&for=pc)

/bin

binBinary的缩写, 这个目录存放着最经常使用的命令。

/boot

这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件。

/dev

devDevice(设备)的缩写, 该目录下存放的是Linux的外部设备,在Linux中访问设备的方式和访问文件的方式是相同的。

/etc

这个目录用来存放所有的系统管理所需要的配置文件和子目录。

/home

用户的主目录,在Linux中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的。

/lib

这个目录里存放着系统最基本的动态连接共享库,其作用类似于Windows里的DLL文件。几乎所有的应用程序都需要用到这些共享库。

/lost+found

这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件。

/media

linux系统会自动识别一些设备,例如U盘、光驱等等,当识别后,linux会把识别的设备挂载到这个目录下。

/mnt

系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在/mnt/上,然后进入该目录就可以查看光驱里的内容了。

/opt

这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。

/proc

这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。

这个目录的内容不在硬盘上而是在内存里,我们也可以直接修改里面的某些文件,比如可以通过下面的命令来屏蔽主机的ping命令,使别人无法ping你的机器:

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

/root

该目录为系统管理员,也称作超级权限者的用户主目录。

/sbin

s就是Super User的意思,这里存放的是系统管理员使用的系统管理程序。

/selinux

这个目录是Redhat/CentOS所特有的目录,Selinux是一个安全机制,类似于windows的防火墙,但是这套机制比较复杂,这个目录就是存放selinux相关的文件的。

/srv

该目录存放一些服务启动之后需要提取的数据。

/sys

这是linux2.6内核的一个很大的变化。该目录下安装了2.6内核中新出现的一个文件系统 sysfs

sysfs文件系统集成了下面3种文件系统的信息:针对进程信息的proc文件系统、针对设备的devfs文件系统以及针对伪终端的devpts文件系统。

该文件系统是内核设备树的一个直观反映。

当一个内核对象被创建的时候,对应的文件和目录也在内核对象子系统中被创建。

/tmp

这个目录是用来存放一些临时文件的。

/usr

这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似与windows下的program files目录。

/usr/bin

系统用户使用的应用程序。

/usr/sbin

超级用户使用的比较高级的管理程序和系统守护程序。

/usr/src:内核源代码默认的放置目录。

/var

这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。

linux系统中,有几个目录是比较重要的,平时需要注意不要误删除或者随意更改内部文件。

/etc 上边也提到了,这个是系统中的配置文件,如果你更改了该目录下的某个文件可能会导致系统不能启动。

/bin, /sbin, /usr/bin, /usr/sbin: 这是系统预设的执行文件的放置目录,比如 ls 就是在/bin/ls 目录下的。

值得提出的是,/bin, /usr/bin 是给系统用户使用的指令(除root外的通用户),而/sbin, /usr/sbin 则是给root使用的指令。

/var 这是一个非常重要的目录,系统上跑了很多程序,那么每个程序都会有相应的日志产生,而这些日志就被记录到这个目录下,具体在/var/log 目录下,另外mail的预设放置也是在这里

 

文件夹创建

mkdir 命令

help' for more information.

[root@com /]# mkdir --help

Usage: mkdir [OPTION]... DIRECTORY...

Create the DIRECTORY(ies), if they do not already exist.

 

Mandatory arguments to long options are mandatory for short options too.

  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask

  -p, --parents     no error if existing, make parent directories as needed

  -v, --verbose     print a message for each created directory

  -Z, --context=CTX  set the SELinux security context of each created

                      directory to CTX

      --help     display this help and exit

      --version  output version information and exit

创建目录: mkdir   /usr/toolsInstall

创建父目录不存在的情况: mkdir  -p /usr/dev/devWorkSpace

文件夹查看

ls命令

ls 命令使用

  -a, --all                  do not ignore entries starting with .

  -A, --almost-all           do not list implied . and ..

      --author               with -l, print the author of each file

  -b, --escape               print octal escapes for nongraphic characters

      --block-size=SIZE      use SIZE-byte blocks.  See SIZE format below

  -B, --ignore-backups       do not list implied entries ending with ~

  -c                         with -lt: sort by, and show, ctime (time of last

                               modification of file status information)

                               with -l: show ctime and sort by name

                               otherwise: sort by ctime

  -C                         list entries by columns

      --color[=WHEN]         colorize the output.  WHEN defaults to `always'

                               or can be `never' or `auto'.  More info below

  -d, --directory            list directory entries instead of contents,

                               and do not dereference symbolic links

  -D, --dired                generate output designed for Emacs' dired mode

  -f                         do not sort, enable -aU, disable -ls --color

  -F, --classify             append indicator (one of */=>@|) to entries

      --file-type            likewise, except do not append `*'

      --format=WORD          across -x, commas -m, horizontal -x, long -l,

                               single-column -1, verbose -l, vertical -C

      --full-time            like -l --time-style=full-iso

  -g                         like -l, but do not list owner

      --group-directories-first

                             group directories before files.

                               augment with a --sort option, but any

                               use of --sort=none (-U) disables grouping

  -G, --no-group             in a long listing, don't print group names

  -h, --human-readable       with -l, print sizes in human readable format

                               (e.g., 1K 234M 2G)

      --si                   likewise, but use powers of 1000 not 1024

  -H, --dereference-command-line

                             follow symbolic links listed on the command line

      --dereference-command-line-symlink-to-dir

                             follow each command line symbolic link

                             that points to a directory

      --hide=PATTERN         do not list implied entries matching shell PATTERN

                               (overridden by -a or -A)

      --indicator-style=WORD  append indicator with style WORD to entry names:

                               none (default), slash (-p),

                               file-type (--file-type), classify (-F)

  -i, --inode                print the index number of each file

  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN

  -k                         like --block-size=1K

  -l                         use a long listing format

  -L, --dereference          when showing file information for a symbolic

                               link, show information for the file the link

                               references rather than for the link itself

  -m                         fill width with a comma separated list of entries

  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs

  -N, --literal              print raw entry names (don't treat e.g. control

                               characters specially)

  -o                         like -l, but do not list group information

  -p, --indicator-style=slash

                             append / indicator to directories

  -q, --hide-control-chars   print ? instead of non graphic characters

      --show-control-chars   show non graphic characters as-is (default

                             unless program is `ls' and output is a terminal)

  -Q, --quote-name           enclose entry names in double quotes

      --quoting-style=WORD   use quoting style WORD for entry names:

                               literal, locale, shell, shell-always, c, escape

  -r, --reverse              reverse order while sorting

  -R, --recursive            list subdirectories recursively

  -s, --size                 print the allocated size of each file, in blocks

  -S                         sort by file size

      --sort=WORD            sort by WORD instead of name: none -U,

                             extension -X, size -S, time -t, version -v

      --time=WORD            with -l, show time as WORD instead of modification

                             time: atime -u, access -u, use -u, ctime -c,

                             or status -c; use specified time as sort key

                             if --sort=time

      --time-style=STYLE     with -l, show times using style STYLE:

                             full-iso, long-iso, iso, locale, +FORMAT.

                             FORMAT is interpreted like `date'; if FORMAT is

                             FORMAT1FORMAT2, FORMAT1 applies to

                             non-recent files and FORMAT2 to recent files;

                             if STYLE is prefixed with `posix-', STYLE

                             takes effect only outside the POSIX locale

  -t                         sort by modification time

  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8

  -u                         with -lt: sort by, and show, access time

                               with -l: show access time and sort by name

                               otherwise: sort by access time

  -U                         do not sort; list entries in directory order

  -v                         natural sort of (version) numbers within text

  -w, --width=COLS           assume screen width instead of current value

  -x                         list entries by lines instead of by columns

  -X                         sort alphabetically by entry extension

  -1                         list one file per line

 

SELinux options:

 

  --lcontext                 Display security context.   Enable -l. Lines

                             will probably be too wide for most displays.

  -Z, --context              Display security context so it fits on most

                             displays.  Displays only mode, user, group,

                             security context and file name.

  --scontext                 Display only security context and file name.

      --help     display this help and exit

      --version  output version information and exit

ll /usr/

展示详细信息(我最喜欢用)

[root@com dataDir]# ll /usr/

total 220

dr-xr-xr-x.   2 root root 69632 Dec 17 23:20 bin

drwxr-xr-x.   2 root root  4096 Sep 23  2011 etc

drwxr-xr-x.   2 root root  4096 Sep 23  2011 games

drwxr-xr-x. 139 root root 12288 Dec 17 01:51 include

dr-xr-xr-x.  29 root root  4096 Dec 13 06:11 lib

dr-xr-xr-x. 112 root root 69632 Dec 17 01:52 lib64

drwxr-xr-x.  20 root root 12288 Dec 17 01:52 libexec

drwxr-xr-x.  13 root root  4096 Mar 17 17:02 local

-rw-r--r--.   1 root root   943 Dec 13 06:33 my.cnf

dr-xr-xr-x.   2 root root 12288 Dec 17 23:20 sbin

drwxr-xr-x. 197 root root 12288 Dec 17 01:52 share

drwxr-xr-x.   4 root root  4096 Dec 13 00:39 src

lrwxrwxrwx.   1 root root    10 Dec 13 00:39 tmp -> ../var/tmp

drwxr-xr-x.   2 root root  4096 Dec 18 05:12 toolsLindPath

 

ll –a -S /usr/

-a显示隐藏文件

-S 指定所有占用空间的大小排序

[root@com dataDir]# ll -a  -S  /usr/

total 228

dr-xr-xr-x.   2 root root 69632 Dec 17 23:20 bin

dr-xr-xr-x. 112 root root 69632 Dec 17 01:52 lib64

drwxr-xr-x. 139 root root 12288 Dec 17 01:51 include

drwxr-xr-x.  20 root root 12288 Dec 17 01:52 libexec

dr-xr-xr-x.   2 root root 12288 Dec 17 23:20 sbin

drwxr-xr-x. 197 root root 12288 Dec 17 01:52 share

drwxr-xr-x.  14 root root  4096 Dec 13 06:33 .

dr-xr-xr-x.  26 root root  4096 Mar 30 20:27 ..

drwxr-xr-x.   2 root root  4096 Sep 23  2011 etc

drwxr-xr-x.   2 root root  4096 Sep 23  2011 games

dr-xr-xr-x.  29 root root  4096 Dec 13 06:11 lib

drwxr-xr-x.  13 root root  4096 Mar 17 17:02 local

drwxr-xr-x.   4 root root  4096 Dec 13 00:39 src

drwxr-xr-x.   2 root root  4096 Dec 18 05:12 toolsLindPath

-rw-r--r--.   1 root root   943 Dec 13 06:33 my.cnf

lrwxrwxrwx.   1 root root    10 Dec 13 00:39 tmp -> ../var/tmp

切换文件夹

cd /dir #进入文件夹

cd .. #返回上级文件夹

cd  #进入用户主目录

 

删除文件夹

rmdir #删除空文件夹

 

rm /dir # 删除指定文件夹 会有删除确认提升

 

rm -rf /dir #强制删除文件夹(谨慎使用,生成系统最好禁止该命令)

 

拷贝文件

cp

  -a, --archive                same as -dR --preserve=all

      --backup[=CONTROL]       make a backup of each existing destination file

  -b                           like --backup but does not accept an argument

      --copy-contents          copy contents of special files when recursive

  -d                           same as --no-dereference --preserve=links

  -f, --force                  if an existing destination file cannot be

                                 opened, remove it and try again (redundant if

                                 the -n option is used)

  -i, --interactive            prompt before overwrite (overrides a previous -n

                                  option)

  -H                           follow command-line symbolic links in SOURCE

  -l, --link                   link files instead of copying

  -L, --dereference            always follow symbolic links in SOURCE

  -n, --no-clobber             do not overwrite an existing file (overrides

                                 a previous -i option)

  -P, --no-dereference         never follow symbolic links in SOURCE

  -p                           same as --preserve=mode,ownership,timestamps

      --preserve[=ATTR_LIST]   preserve the specified attributes (default:

                                 mode,ownership,timestamps), if possible

                                 additional attributes: context, links, xattr,

                                 all

  -c                           same as --preserve=context

      --no-preserve=ATTR_LIST  don't preserve the specified attributes

      --parents                use full source file name under DIRECTORY

  -R, -r, --recursive          copy directories recursively

      --reflink[=WHEN]         control clone/CoW copies. See below.

      --remove-destination     remove each existing destination file before

                                 attempting to open it (contrast with --force)

      --sparse=WHEN            control creation of sparse files. See below.

      --strip-trailing-slashes  remove any trailing slashes from each SOURCE

                                 argument

  -s, --symbolic-link          make symbolic links instead of copying

  -S, --suffix=SUFFIX          override the usual backup suffix

  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY

  -T, --no-target-directory    treat DEST as a normal file

  -u, --update                 copy only when the SOURCE file is newer

                                 than the destination file or when the

                                 destination file is missing

  -v, --verbose                explain what is being done

  -x, --one-file-system        stay on this file system

  -Z, --context=CONTEXT        set security context of copy to CONTEXT

      --help     display this help and exit

      --version  output version information and exit

eg: cd /test.txt /usr/

-v 显示执行命令过程

-r 负责目录下所有目录文件(包括子目录)

服务器间拷贝文件

scp

本地拷贝到其他服务器

scp /usr/local/test.txt [email protected]:/opt

scp -R /usr/local/mydata [email protected]:/usr

-R 负责目录下所有文件包括子目录

从其他服务器拷贝到本地

scp -r [email protected]:/dataDir/es/log ./

 

创建文件

couch /usr/test.txt #创建文件

vi fileName

vim fileName

查看文件

cat filename

 

移动文件或者修改文件名称

mv filename /filePath

mv filename filename2

查看文件夹路径

pwd #查看文件夹路径

 

搜索文件

locate fileName

如果没有此命令使用

yum -y install mlocate #安装

 

搜索文件总内容

grep #搜索特定字符串来显示。

grep start* project57.log

[2019-03-30T03:06:30,706][INFO ][o.e.n.Node               ] [es_1] starting ...

[2019-03-30T03:06:34,431][INFO ][o.e.n.Node               ] [es_1] started

[2019-03-30T03:06:35,787][INFO ][o.e.c.r.a.AllocationService] [es_1] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[packets-2019-03-21][0], [packets-2019-03-21][6]] ...]).

参数说明

重导命令

将某命令结果输出到文件中。

> 将结果输出到文件中,并将原有内容覆盖

>> 将结果追加到文件中

 

grep -i start* project57.log > sucess.txt

[root@com log]# ll

total 16

-rw-rw-r-- 1 es   es   3349 Mar 26 04:44 project57-2019-03-26.log

-rw-rw-r-- 1 es   es    268 Mar 30 03:06 project57_deprecation.log

-rw-rw-r-- 1 es   es      0 Mar 26 03:00 project57_index_indexing_slowlog.log

-rw-rw-r-- 1 es   es      0 Mar 26 03:00 project57_index_search_slowlog.log

-rw-rw-r-- 1 es   es   3066 Mar 30 03:06 project57.log

-rw-r--r-- 1 root root  359 Mar 30 22:33 sucess.txt

[root@com log]# cat sucess.txt

[2019-03-30T03:06:30,706][INFO ][o.e.n.Node               ] [es_1] starting ...

[2019-03-30T03:06:34,431][INFO ][o.e.n.Node               ] [es_1] started

[2019-03-30T03:06:35,787][INFO ][o.e.c.r.a.AllocationService] [es_1] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[packets-2019-03-21][0], [packets-2019-03-21][6]] ...]).

覆盖结果:

grep -i Warn* project57_deprecation.log >sucess.txt

[root@com log]# cat sucess.txt

[2019-03-26T03:00:22,032][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

[2019-03-30T03:06:25,845][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

追加结果

grep -i start* project57.log >> sucess.txt

[root@com log]# cat sucess.txt

[2019-03-26T03:00:22,032][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

[2019-03-30T03:06:25,845][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

[2019-03-30T03:06:30,706][INFO ][o.e.n.Node               ] [es_1] starting ...

[2019-03-30T03:06:34,431][INFO ][o.e.n.Node               ] [es_1] started

[2019-03-30T03:06:35,787][INFO ][o.e.c.r.a.AllocationService] [es_1] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[packets-2019-03-21][0], [packets-2019-03-21][6]] ...]).

管道命令

管道命令(pipe)’|’ ,将结果输给另外一个命令

ll -a | grep project57.log  >> sucess.txt

[root@com log]# cat sucess.txt

[2019-03-26T03:00:22,032][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

[2019-03-30T03:06:25,845][WARN ][o.e.d.s.g.GroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead

[2019-03-30T03:06:30,706][INFO ][o.e.n.Node               ] [es_1] starting ...

[2019-03-30T03:06:34,431][INFO ][o.e.n.Node               ] [es_1] started

[2019-03-30T03:06:35,787][INFO ][o.e.c.r.a.AllocationService] [es_1] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[packets-2019-03-21][0], [packets-2019-03-21][6]] ...]).

-rw-rw-r-- 1 es   es   3066 Mar 30 03:06 project57.log

 

 

你可能感兴趣的:(linux-centos)