There’s no way to cut it when it comes to telling you how to use this great tool!
Whenever I’ve used it, I commonly use it in lieu of a user’s home directory: $(pwd | cut -d\/ -f5)
. This helps me save time from copy and pasting the install name, however it doesn’t always work.
For example, if we’re not in /path/to/live/SITE
or /path/to/log/SITE
then cut can’t grab the appropriate path. Let me break it down by first running pwd.
spencer@local:/nas/content/live/dubtraxx/wp-content/themes/minimum-pro$ pwd /nas/content/live/dubtraxx/wp-content/themes/minimum-pro
We see that it’s printing
the working directory
, all the way up to the directory we’re in. PWD is just printing the folder I’m currently in, including the server’s absolute path. Whenever you include the -d
you’re telling pwd
that you want to use /
as your delimiter (separator).
However, since /
is a special character in Linux’s bash shell, we need to escape it by adding a backslash \
.
On this note, it’s also perfectly acceptable to use -d'/'
instead of a \
to signal the delimiter of choice to cut
.
Let’s take a look at what happens if we run the following:
spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f2 path spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f3 to spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f4 live spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f5 dubtraxx
And for some testing…
spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d'/' -f4- live/dubtraxx spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f2- path/to/live/dubtraxx spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f1 spencer@local~ :/path/to/live/dubtraxx $ pwd | cut -d\/ -f1- /path/to/live/dubtraxx
There are many more uses for cut, but we’ll go over those in another tutorial.
Ciao!