{"id":13511,"date":"2026-02-11T01:01:00","date_gmt":"2026-02-11T06:01:00","guid":{"rendered":"https:\/\/www.both.org\/?p=13511"},"modified":"2026-02-10T21:29:17","modified_gmt":"2026-02-11T02:29:17","slug":"getting-started-with-the-cd-command","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=13511","title":{"rendered":"Getting Started With the cd Command"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"13511\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p>The <strong>cd<\/strong> command is used on the Linux command line to change from one directory to another. When working at the command line, moving between directories is a common task which can result in a significant reduction in typing long path names. As a <a href=\"https:\/\/www.both.org\/?p=7200\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=7200\" target=\"_blank\" rel=\"noreferrer noopener\">Lazy SysAdmin<\/a>, reducing the amount of typing I do makes my work easier and me more productive.<\/p>\n\n\n\n<p>Like many Linux, and Unix, commands it&#8217;s name is short and meaningful, &#8220;<strong>c<\/strong>hange <strong>d<\/strong>irectory.&#8221; This article looks at how the <strong>cd<\/strong> command works as a regular, non-root user, and some of its interesting yet lesser known but time-saving capabilities.<\/p>\n\n\n\n<p>First, let&#8217;s look at the storage structure that makes commands like <strong>cd<\/strong> necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Directory Tree<\/h2>\n\n\n\n<p>Like most operating systems, Linux uses a tree-like structure for data storage. The tree starts at the root directory (\/) and branches off into sub-directories that provide a means to organize the files on a computer. <\/p>\n\n\n\n<p>The structure used by Linux was first designed and implemented on Unix in 1969, and it&#8217;s codified in the Linux <a href=\"https:\/\/www.both.org\/?p=6082\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=6082\" target=\"_blank\" rel=\"noreferrer noopener\">Filesystem Hierarchical Standard<\/a> (FHS). For this article, we&#8217;re only interested in the home directory for a single user, me. <\/p>\n\n\n\n<p>You can see below, the result of a <strong>tree<\/strong> command for the \/home directory which contains the home directory for my test user as well as the home directories myself and for other users if there were any. The <strong>d<\/strong> option specifies that we want to see only directories, and the <strong>L2<\/strong> option specifies that the filesystem tree will only be listed to 2 levels deep which significantly reduces the amount of output from the command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>tree -dL2 \/home<\/strong>\n\/home\n\u251c\u2500\u2500 dboth  &#91;error opening dir]\n\u251c\u2500\u2500 lost+found  &#91;error opening dir]\n\u2514\u2500\u2500 tuser1\n    \u251c\u2500\u2500 Desktop\n    \u251c\u2500\u2500 Documents\n    \u251c\u2500\u2500 Downloads\n    \u251c\u2500\u2500 Music\n    \u251c\u2500\u2500 Pictures\n    \u251c\u2500\u2500 Public\n    \u251c\u2500\u2500 Templates\n    \u2514\u2500\u2500 Videos\n\n12 directories<\/code><\/pre>\n\n\n\n<p>You can see from this, the very basic default directory structure for a newly created user. This is what you&#8217;d see for a new user on most distributions so you wouldn&#8217;t need the <strong>L2<\/strong> option. Most of us will need that when exploring our own home directories.<\/p>\n\n\n\n<p>Note that the test user cannot access the contents of the other two directories in \/home, as they don&#8217;t have permissions to do so. <\/p>\n\n\n\n<p>You might want to experiment with the options on this command for your own system so you can see the full depth of the directory structure as well as the files it contains. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a> The PWD<\/h2>\n\n\n\n<p>The term \u201cPWD\u201d refers to present working directory, which you might know as the \u201ccurrent directory.\u201d The PWD is important because all command actions take place in the PWD unless another location is explicitly specified in the command. The <strong>pwd<\/strong> command means \u201cprint working directory,\u201d that is, print the name of the current directory on the terminal display. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>pwd<\/strong>\n\/home\/tuser1\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>Directory path notation styles<\/h2>\n\n\n\n<p>A path is a notational method for referring to directories in the Linux directory tree. This gives us a method for expressing the path to a directory or a file that is not in the pwd. Linux uses paths extensively for easy location of and access to executable files, making it unnecessary to type the entire path to the executable.<\/p>\n\n\n\n<p>For example, it is easier to type \u201cls\u201d than it is to type \u201c\/usr\/bin\/ls\u201d to run the ls command. The shell uses the PATH variable where it finds a list of directories in which to search for the executable by the name \u201cls\u201d.<\/p>\n\n\n\n<p>This simple experiment simply displays the content of the PATH environment variable for the test user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>echo $PATH<\/strong>\n\/home\/tuser1\/.local\/bin:\/home\/tuser1\/bin:\/usr\/lib64\/qt-3.3\/bin:\/usr\/local\/bin:\/usr\/bin\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<p>The various paths \u2013 directories \u2013 that the shell will search are listed in the output from the above command. Each path is separated by a colon (:).<\/p>\n\n\n\n<p>There are two types of notation we can use to express a path \u2013 absolute and relative. An absolute path is specified completely starting with the root directory. So if the PWD is the Downloads directory of my home directory, I would specify the absolute path as \/home\/student\/Downloads. With that as my PWD, if I need to specify the absolute path to my Documents\/Work directory as \/home\/student\/Documents\/Work. I could also specify that path in relative notation from my current PWD as, ..\/Documents\/Work. I could also use the notation, ~\/Documents\/Work because the Tilde (~) is a shorthand notation for my home directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>Moving around the directory tree<\/h2>\n\n\n\n<p>Let\u2019s start by looking at how to move around the Linux filesystem directory tree at the command line. Many times working on or in a directory is easier if it is the PWD. Moving around the filesystem is a very important capability and there are a number of shortcuts that can help as well.<\/p>\n\n\n\n<p>Perform this little experiment as a test user, tuser1 in my case. You should already be logged into your computer with terminal session open as your test user. If not, do that now. <\/p>\n\n\n\n<p>Moving around the Linux filesystem directory tree is important for many reasons. Start by using the <strong>pwd<\/strong> command to display the PWD.  <\/p>\n\n\n\n<p>The first time I checked, the pwd was the \/tmp directory because I had been working there. Your PWD will probably be your home directory, (~). Using the <strong>cd<\/strong> command with no options always makes your home directory the PWD. Notice in the command prompt that the tilde (~) is a shorthand indicator for your home directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/tmp$ <strong>pwd<\/strong>\n\/tmp\ntuser1@essex:\/tmp$ <strong>cd<\/strong>\ntuser1@essex:~$ <strong>pwd<\/strong>\n\/home\/tuser1\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<p>Now just do a simple command, long list, to view the content of your home directory. These directories are created when a new user logs in to the account using the GUI for the first time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>ls -l<\/strong>\ntotal 32\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Desktop\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Documents\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Downloads\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Music\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Pictures\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Public\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Templates\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Videos\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<p>This command does not show the so-called hidden files in your home directory which makes it easier to scan the rest of the contents.<\/p>\n\n\n\n<p>Let\u2019s create a few files to work with since there are none other than the hidden configuration files created by default. The following command line program will create 50 text files that have a bit of data in them so we have more than just directories to look at. Enter the program all on one line. It is OK if the line wraps on your screen, just do not press <strong>Enter<\/strong> until you have completed the entire command. Then list the contents of the home directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>for X in `seq -w 1 50` ; do echo \"This is file $X.txt\" > file$X.txt ; done<\/strong>\ntuser1@essex:~$ <strong>ls<\/strong>\nDesktop     file04.txt  file10.txt  file16.txt  file22.txt  file28.txt  file34.txt  file40.txt  file46.txt  Pictures\nDocuments   file05.txt  file11.txt  file17.txt  file23.txt  file29.txt  file35.txt  file41.txt  file47.txt  Public\nDownloads   file06.txt  file12.txt  file18.txt  file24.txt  file30.txt  file36.txt  file42.txt  file48.txt  Templates\nfile01.txt  file07.txt  file13.txt  file19.txt  file25.txt  file31.txt  file37.txt  file43.txt  file49.txt  Videos\nfile02.txt  file08.txt  file14.txt  file20.txt  file26.txt  file32.txt  file38.txt  file44.txt  file50.txt\nfile03.txt  file09.txt  file15.txt  file21.txt  file27.txt  file33.txt  file39.txt  file45.txt  Music\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<p>Now do a long listing of the directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>ls -l<\/strong>\ntotal 232\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Desktop\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Documents\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Downloads\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file01.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file02.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file03.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file04.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file05.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file06.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file07.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file08.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file09.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file10.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file11.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file12.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file13.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file14.txt\n&lt;SNIP>\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file46.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file47.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file48.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file49.txt\n-rw-r--r-- 1 tuser1 tuser1   20 Feb 10 16:43 file50.txt\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Music\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Pictures\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Public\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Templates\ndrwxr-xr-x 2 tuser1 tuser1 4096 Feb  9 08:20 Videos\ntuser1@essex:~$<\/code><\/pre>\n\n\n\n<p>This long listing shows the ownership and <a href=\"https:\/\/www.both.org\/?p=7999\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=7999\" target=\"_blank\" rel=\"noreferrer noopener\">file permissions<\/a> &#8212; also called the file mode &#8212; for each file and directory. The permissions  drwxr-xr-x show first that this is a directory with the leading \u201cd\u201d while files have a dash (-) in that position. The file permissions are three triplets of (R)ead, (W)rite, and e(X)ecute. Each triplet represents User, the owner of the file, Group, the group that owns the file, and Other, for all other users. These permissions represent something a bit different on a directory. <\/p>\n\n\n\n<p>Make \/var\/log the pwd and list the contents.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~$ <strong>cd \/var\/log<\/strong>\ntuser1@essex:\/var\/log$ ls -l \ntotal 67348\ndrwxrwxr-x  2 root    akmods  4096 Feb  1 00:34 akmods\ndrwxr-xr-x. 2 root    root    4096 Jan 19 09:03 anaconda\n-rw-r--r--  1 root    root     380 Feb  9 15:07 apcupsd.events\n-rw-r--r--  1 root    root      82 Jan 25 00:38 apcupsd.events-20260125\n-rw-r--r--  1 root    root    1263 Feb  1 00:34 apcupsd.events-20260201\n-rw-r--r--  1 root    root     190 Feb  8 00:31 apcupsd.events-20260208\ndrwxr-xr-x. 2 root    root    4096 Sep 22 20:00 atop\ndrwx------. 2 root    root    4096 Jan 31 19:00 audit\ndrwxr-xr-x. 2 root    root    4096 Oct  5 20:00 blivet-gui\n&lt;SNIP>\ndrwxr-xr-x. 2 lightdm lightdm 4096 Feb  9 15:07 lightdm\ndrwxr-xr-x. 2 root    root    4096 Jan 19 09:27 mail\n-rw-------  1 root    root   16358 Feb 10 03:28 maillog\n-rw-------. 1 root    root   36582 Jan 25 00:00 maillog-20260125\n-rw-------  1 root    root   42845 Feb  1 00:00 maillog-20260201\n-rw-------  1 root    root   39499 Feb  8 00:00 maillog-20260208\n-rw-------  1 root    root 7991182 Feb 10 19:01 messages\n-rw-------. 1 root    root 9852761 Jan 25 00:38 messages-20260125\n-rw-------  1 root    root 8093136 Feb  1 00:34 messages-20260201\n-rw-------  1 root    root 6194218 Feb  8 00:31 messages-20260208\n&lt;SNIP>\nlrwxrwxrwx. 1 root    root      39 Jan 19 08:59 README -> ..\/..\/usr\/share\/doc\/systemd\/README.logs\ndrwxr-xr-x. 2 root    root    4096 Feb  8 00:31 rkhunter\ndrwxr-xr-x. 2 root    root    4096 Feb 10 00:07 sa\ndrwx------. 3 root    root    4096 Jan 24 19:00 samba\n-rw-------  1 root    root   18431 Feb 10 15:11 secure\n-rw-------. 1 root    root   37914 Jan 25 00:20 secure-20260125\n-rw-------  1 root    root   47704 Feb  1 00:17 secure-20260201\n-rw-------  1 root    root   23208 Feb  8 00:17 secure-20260208\ndrwx------. 2 root    root    4096 Sep 18 20:00 speech-dispatcher\n-rw-------  1 root    root       0 Feb  8 00:31 spooler\n-rw-------. 1 root    root       0 Jan 19 09:00 spooler-20260125\n-rw-------  1 root    root       0 Jan 25 00:38 spooler-20260201\n-rw-------  1 root    root       0 Feb  1 00:34 spooler-20260208\ndrwxrwx---. 2 sssd    sssd    4096 Feb  8 00:31 sssd\n-rw-rw-r--. 1 root    utmp  140160 Feb 10 15:11 wtmp\n-rw-r--r--  1 root    root   87901 Feb 10 18:56 Xorg.0.log\n-rw-r--r--  1 root    root   67064 Feb  9 15:05 Xorg.0.log.old\n-rw-r--r--  1 root    root   46342 Feb  9 14:53 Xorg.1.log\n-rw-r--r--  1 root    root   96904 Feb  9 14:52 Xorg.1.log.old\n-rw-r--r--  1 root    root   46333 Feb  9 08:21 Xorg.2.log\ntuser1@essex:\/var\/log$<\/code><\/pre>\n\n\n\n<p>Try to display the content of the messages file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/var\/log$ <strong>cat messages<\/strong>\ncat: messages: Permission denied\ntuser1@essex:\/var\/log$<\/code><\/pre>\n\n\n\n<p>If you are using Fedora like I do, there should be a README file in \/var\/log. Other distros should also have that README file. Just to do something while here, use the <strong>cat<\/strong> command to view the contents of this file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/var\/log$ cat README\nYou are looking for the traditional text log files in \/var\/log, and they are gone?\n\nHere's an explanation on what's going on:\n&lt;SNIP><\/code><\/pre>\n\n\n\n<p>Now change to the Documents sub-directory of your home directory (~).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/var\/log$ <strong>cd ~\/Documents\/ ; ls -l<\/strong>\ntotal 0\ntuser1@essex:~\/Documents$<\/code><\/pre>\n\n\n\n<p>Notice that I used the tilde (~) to represent the home directory which would otherwise have to be typed out as \/home\/student\/Documents. When I want to return to the \/var\/log directory I can save a bit of typing using this next shortcut. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:~\/Documents$ <strong>cd -<\/strong>\n\/var\/log\ntuser1@essex:\/var\/log$<\/code><\/pre>\n\n\n\n<p>The dash (-), aka, the minus sign, will always return you to the previous PWD. How? Look a bit at the environment which defines many environment variables including $PWD and $OLDPWD. The env command prints all of the current environment variables and the grep command extracts and sends to STDOUT only those lines that contain \u201cPWD\u201d.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/var\/log$ <strong>env | grep -i PWD<\/strong>\nPWD=\/var\/log\nOLDPWD=\/home\/tuser1\/Documents\ntuser1@essex:\/var\/log$<\/code><\/pre>\n\n\n\n<p>The dash (-), when used as an option to the cd command, is a shorthand notation for the $OLDPWD variable. The command could also be issued in the following manner.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tuser1@essex:\/var\/log$ <strong>cd $OLDPWD<\/strong>\ntuser1@essex:~\/Documents$<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The <strong>cd<\/strong> command looks simple on the surface but it has some interesting capabilities that make moving around the Linux directory tree much easier if you know them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The cd command is used on the Linux command line to change from one directory to another. This article looks at how it works and some of its interesting yet less well-known capabilities.<\/p>\n","protected":false},"author":2,"featured_media":3182,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,503],"tags":[505,924,156],"class_list":["post-13511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-linux-101","tag-basic-linux-commands","tag-cd","tag-cli"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/13511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13511"}],"version-history":[{"count":32,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/13511\/revisions"}],"predecessor-version":[{"id":13564,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/13511\/revisions\/13564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3182"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}