- Raspberry Pi Zero Cookbook
- Edward Snajder
- 1239字
- 2021-07-09 19:12:13
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...
- 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 theadduser
command. - 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
- 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
- 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 withls -help
and explained in more detail withman 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
- My typical command when I start looking in a directory is this one:
ls -ltrh
- 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
- 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 thetail
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 pipels -ltrh
to thetail
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
- What about the five largest files? Instead of the
t
in-ltrh
, I can useS
: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
- 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 usetree
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
- Last, we will look at a couple of searching utilities,
find
andgrep
. Thefind
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 runtree
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 withcowbell
in the name. I can use thefind
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, thefind
command returns the exactly location of anything that matches. There are tons of options for using thefind
command; start withfind -help
, and then tryman find
when you want to get really deep. - 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 likefind
can, but generally,find
is more efficient at finding filename patterns thangrep
is. - If I use
grep
to look for cowbells in mysonic-pi
directory, I'll get a different, and more colorful, output. Runninggrep -r -i cowbell /opt/sonic-pi/*
will return something similar to the screenshot belowNote
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 usingfind
, but we find every file that containscowbell
inside of it. The-r
flag tellsgrep
to delve into subdirectories, and-i
tells it to ignore cases withcowbells
(soCowbell
andcowbell
are both found, as shown in the screenshot). - As you use Linux more often, both
find
andgrep
become regularly used tools for administration and file management. This won't be the last time you use them!
- 大數(shù)據(jù)技術(shù)基礎(chǔ)
- PostgreSQL 11 Server Side Programming Quick Start Guide
- 精通Excel VBA
- 傳感器技術(shù)應(yīng)用
- Data Wrangling with Python
- 完全掌握AutoCAD 2008中文版:綜合篇
- 網(wǎng)絡(luò)化分布式系統(tǒng)預(yù)測(cè)控制
- 數(shù)據(jù)掘金
- 基于單片機(jī)的嵌入式工程開(kāi)發(fā)詳解
- 分?jǐn)?shù)階系統(tǒng)分析與控制研究
- 貫通Java Web開(kāi)發(fā)三劍客
- 單片機(jī)C語(yǔ)言程序設(shè)計(jì)完全自學(xué)手冊(cè)
- Mastering Geospatial Analysis with Python
- 精通LabVIEW程序設(shè)計(jì)
- Linux Shell Scripting Cookbook(Third Edition)