官术网_书友最值得收藏!

Navigating a filesystem and viewing and searching the contents of a directory

If you aren't already a Linux or Mac user, getting around the filesystem can seem pretty alien at first. Truly, if you've only used Windows Explorer, this is going to seem like a strange, alien process. Once you start getting the hang of things, though, you'll find that getting around the Linux filesystem is easy and fun.

Getting ready

The only thing you need to get started is a client connection to your Raspberry Pi Zero. I like to use SSH, but you can certainly connect using the serial connection or a terminal in X Windows.

How to do it...

  1. If you want to find out where you are in the filesystem, use pwd:
            pi@rpz14101:~$ pwd 
            /home/pi
    
    Note

    This tells me I'm in the /home/pi directory, which is the default home directory for the pi user. Generally, every user you create should get a /home/username directory to keep their own files in. This can be done automatically with user creation and the adduser command.

  2. To look at the contents of the directory you are in, use the ls command:
    pi@rpz14101:~$ ls 
    Desktop Downloads Pictures python_games share Videos
    Documents Music Public Scratch Templates
    
  3. To look in another directory, simply specify the directory you want to list (you may need to use sudo depending on where you are looking):
    pi@rpz14101:~$ sudo ls /opt/ 
    cookbook.share pigpio sonic-pi vc
    minecraft-pi share testsudo.deleteme Wolfram
    
  4. This is a nice quick summary of what files are in the directory, but you will usually want a little more information about the files and directories. The ls command has a ton of options, all of which can be displayed with ls -help and explained in more detail with man ls. Some of the best ones to know are as follows:
    • -a show all files (regular and hidden)
    • -l show long format (more file information, in columns)
    • -h human readable (turns bytes into MB or GB as appropriate)
    • -t or -tr order in time order, or reverse time order
  5. My typical command when I start looking in a directory is this one:
              ls -ltrh
    
  6. This produces all non-hidden files, with human-readable sizes, in column format, and the newest file at the bottom:
    pi@rpz14101:~$ ls -ltrh /opt/
    total 513M
    drwxr-xr-x 7 root root 4.0K May 27 04:11 vc
    drwxr-xr-x 3 root root 4.0K May 27 04:32 Wolfram
    drwxr-xr-x 3 root root 4.0K May 27 04:34 pigpio
    drwxr-xr-x 4 root root 4.0K May 27 04:36 minecraft-pi
    drwxr-xr-x 5 root root 4.0K May 27 04:36 sonic-pi
    -rw-r--r-- 1 root root 0 Jul 4 13:41 testsudo.deleteme
    drwxr-xr-x 2 root root 4.0K Jul 9 13:05 share
    -rwxr-xr-x 1 root root 512M Jul 24 17:53 cookbook.share
    
  7. One last trick: if you need this format but there are a lot of files in the directory you are searching, you will see a ton of text scroll by. Maybe you just need the most recent or largest files? We can do this with a pipe (|) and the tail command. Let's take a directory with a lot of files, such as /usr/lib/. To list the five most recently modified files, I can pipe ls -ltrh to the tail command:
    pi@rpz14101:~$ ls -ltrh /usr/lib/ | tail -5
    lrwxrwxrwx 1 root root 22 May 27 04:40 libwiringPiDev.so -> libwiringPiDev.so.2.32
    drwxr-xr-x 2 root root 4.0K Jun 5 10:38 samba
    drwxr-xr-x 3 root root 4.0K Jul 4 22:48 pppd
    drwxr-xr-x 65 root root 60K Jul 24 15:48 arm-linux- gnueabihf
    drwxr-xr-x 2 root root 4.0K Jul 24 15:48 tmpfiles.d
    
  8. What about the five largest files? Instead of the t in -ltrh, I can use S:
    pi@rpz14101:~$ ls -lSrh /usr/lib/ | tail -5
    -rw-r--r-- 1 root root 2.8M Sep 17 2014 libmozjs185.so.1.0.0
    -rw-r--r-- 1 root root 2.8M Sep 30 2014 libqscintilla2.so.11.3.0
    -rw-r--r-- 1 root root 2.9M Jun 5 2014 libcmis-0.4.so.4.0.1
    -rw-r--r-- 1 root root 3.4M Jun 12 2015 libv8.so.3.14.5
    -rw-r--r-- 1 root root 5.1M Aug 18 2014 libmwaw-0.3.so.3.0.1
    
  9. A little creative piping and you can find exactly the file you are looking for. If not, another great tool for exploring the filesystem is tree. This gives a pseudo-graphical tree that shows how the files are structured in the system. It produces a lot of text, especially if you have it print an entire directory tree. If just looking into directory structures, you can use tree with the -d flag for directories only. The -L flag will reduce how deep you pe into nested directories:
            pi@rpz14101:~$ tree -d -L 2 /opt/
            /opt/
            ├── minecraft-pi
            │ ├── api
            │ └── data
            ├── pigpio
            │ └── cgi
            ├── share
            ├── sonic-pi
            │ ├── app
            │ ├── bin
            │ └── etc
            ├── vc
            │ ├── bin
            │ ├── include
            │ ├── lib
            │ ├── sbin
            │ └── src
            └── Wolfram
     └── WolframEngine
    
  10. Last, we will look at a couple of searching utilities, find and grep. The find command is a powerful function that finds files in whatever directories you specify. It is great for trying to find that mystery piece of software that installed itself in an odd place or the needle-in-a-haystack file in a directory that contains hundreds of files. For example, if I were to run tree in the /opt/sonic-pi/ directory, it would run on for several seconds, and thousands of files would shoot by. I, however, am only interested in finding files with cowbell in the name. I can use the find command to look for it:
            pi@rpz14101:~$ find /opt/sonic-pi/ -name *cowbell*
            /opt/sonic-pi/etc/samples/drum_cowbell.flac
    

    When looking for anything with cowbell in the filename, the find command returns the exactly location of anything that matches. There are tons of options for using the find command; start with find -help, and then try man find when you want to get really deep.

  11. The grep command can be used in a couple different ways when searching for files, and it is one of those commands you will find yourself using constantly while both loving and hating its awesome power. Let's say you need to find something inside of a file -grep is the tool for you. It can also find things like find can, but generally, find is more efficient at finding filename patterns than grep is.
  12. If I use grep to look for cowbells in my sonic-pi directory, I'll get a different, and more colorful, output. Running grep -r -i cowbell /opt/sonic-pi/* will return something similar to the screenshot below
    Note

    Wrapping your search string in double quotes is a good practice to stick to in the long run. In this case, we would use grep -r -i "cowbell" /opt/sonic-pi/*. When moving into more complex regular expressions, and strings that use special characters, the double-quotes will help a lot.

    We don't see the file with cowbell in the name like we did using find, but we find every file that contains cowbell inside of it. The -r flag tells grep to delve into subdirectories, and -i tells it to ignore cases with cowbells (so Cowbell and cowbell are both found, as shown in the screenshot).

  13. As you use Linux more often, both find and grep become regularly used tools for administration and file management. This won't be the last time you use them!
主站蜘蛛池模板: 天镇县| 金寨县| 黔南| 河东区| 秦皇岛市| 比如县| 资中县| 桐城市| 新和县| 石门县| 肇东市| 噶尔县| 农安县| 平泉县| 望江县| 榆林市| 同江市| 喜德县| 日喀则市| 清流县| 张北县| 余姚市| 武宁县| 鄂州市| 措勤县| 田阳县| 宜兰县| 东乌珠穆沁旗| 拜泉县| 甘谷县| 望都县| 浙江省| 运城市| 清水河县| 邢台县| 太白县| 河池市| 垦利县| 翼城县| 奈曼旗| 彭阳县|