5

A while ago, I moved from a purely Windows environment to using Windows and a Mac.

Whilst I am slowly getting used to my Mac and loving it, there are a few things that frustrate me.

One of them is the fact that there is only 1 working directory on the filesystem.

That may seem odd to those only ever run Unix, but follow this as my experience on Windows.

I have C: (programs), D: (data), E: (CD/DVD) and F: & G: (USB keys).

If I type ...

CD /D F:\Dir1\Dir2
CD /D D:\Dir3\Dir4
F:
CD
D:
CD

assuming that those are all valid paths, the output will be ...

F:\Dir1\Dir2
D:\Dir3\Dir4

This shows I can swap between "drives" and I get the last working directory per drive.

This is really useful when you want to copy content from one drive to another, without having to type the entire path.

You can go to the directory, verify its contents, switch to the destination drive/directory and then initiate the copy ...

COPY F:*.* D:.

for example.

When it comes to my Mac, whilst I could use the UI with 2 Finder tabs, I would still like to be able to to use the command line.

Is there an equivalent way of having a mounted filesystem remember and expose the last working directory on that filesystem?

At the moment it is the "type the entire command out completely" approach.

9
  • en.m.wikipedia.org/wiki/Pushd_and_popd maybe?
    – HBruijn
    Commented May 31, 2015 at 8:00
  • As a variation, you may try cd /one/path; cd /second/path; cp $OLDPWD/* .
    – Hi-Angel
    Commented May 31, 2015 at 8:03
  • The whole idea of filesystem structure in Unix is unformity, /share/ as local and /share/ mounted from NAS should be equivalent, so no, because it breaks philosophy. But you can use pushd/popd like HBruijn suggests, or do better -- write a shell script and keep your paths as variables.
    – myaut
    Commented May 31, 2015 at 8:06
  • Another gotcha to watch out for: *.* in Unix is any file with at least one dot, in Microsoft's windows it is any file. *.*.* is any file with 2 dots, Windows with one dot. *.*.*.* file with 3 dots, Windows file with 2 dots. etc. Commented May 31, 2015 at 10:07
  • Note bash has excellent history tools. You can recall commands that you typed weeks ago (useful when you can not remember how you did it, or to save typing). This dose not work to well when the context is missing, e.g. cp e: f: If you have cp /path/to/file /path/to/directory then it is repeatable. Commented May 31, 2015 at 10:23

7 Answers 7

6

You should be aware that the Windows way (actually rather the DOS way) is a consequence of having a hard mapping to those low level "devices" that are represented by characters (A:, C:, etc.).

Unix has (basically) only one directory tree and you access it more uniformly than on DOS. You have various possibilities to access the file system. The one that you may know from the DOS world are graphical, window oriented; see for example xfe for one candidate. The other, more "native" access is through the shell. The various shells have different capabilities. You may want (for heavy interactive use) have a look into tcsh, but in the commonly used POSIX shells (bash, ksh, zsh) you have indeed the concept of a working directory. You can navigate between directories with cd and a specific variant is cd - to switch to the previous one. From one directory you can access the previous one using either predefined shell variables $OLDPWD or the respective tilde expression ~- (which resembles the above mentioned cd -). With those you can, say, cp ./* ~-. The ksh has yet a more powerful cd that makes it easy to jump between the respective same subdirectories of different version trees; e.g., cd gawk-3.1.6 gawk-4.1.2 jumps from within any subdirectory of those variants to the respective other one. In some shells there's also two commands pushd and popd commands available (either natively or through a library) to memorize directories that you want to revisit. Then there's the concept of CDPATH, allowing you to jump to commonly used directories without fully specifying the full paths. - I wrote all that about changing directories because it's the base to make best use of the $OLDPWD or resp. ~- that I mentioned before in your shell commands. The description would not be complete without the command and path completion features mentioned; usually with the Tab key you can complete commands, so that even if you explicitly "write" the paths, the shell will help you to do that very quickly. And last but not least there's the shell history; you have access to previously typed commands, may edit and/or re-invoke them as desired.

4

Your question is about the features of bash, which is probably the shell you are using as command line.

For the switching part you can use cd - to switch back to the previous directory.

If you want to reference that previous directory in a command you can use $OLDPWD like this:

 cp $OLDPWD/* .

Also have a look at help pushd, help popd and generally man bash.

4

I will add another option/trick.

It uses symbolic links, these are very useful. They are a bit like short-cuts but, in most ways, better (except where short-cuts are used as wrapper scripts, in which case wrapper scripts are better).

It also uses bash's CDPATH variable.

cd ~
mkdir drive-letters
cd drive-letters
ln -s / c:
ln -s "$HOME" h:
ln -s /media/cdrom/ e:
CDPATH=".:~/drive-letters" # you will have to put this one in your ~/.bashrc to make it persistent

Now you can do:

cd c:
cd e:
2
  • This is really useful. Commented Jun 4, 2015 at 9:31
  • my solution: ln -s /mnt/hda /c ; ln -s /mnt/hdb /d , etc. Now instead of C:\Windows\notepad.exe you have /c/Windows/notepad.exe (quicker to type and works in all apps)
    – eadmaster
    Commented Aug 30, 2015 at 4:43
3

I am going to suggest a solution which will not solve all of your problems, but will help you to switch way way faster!!

The solution is using autojump.

Install it on Mac using:

 brew install autojump

Then, what autojump does is it remembers all of your directories you visited previously. It keeps score for various directories, so that the directory most visited gets a higher score.

Then, you quickly switch to a directory using a simple command in the following fashion:

#Say you want to visit this:
cd /media/windows/users/myuserdir

#Instead do this: 
j myuserdir

How it does this is it matches the keyword myuserdir with a directory containing the keyword and having the highest score. Even if you make some spelling mistake, it will try to find the best match based on your history.

2

If you are referring to physical partitions these are mounted on /Volumes/PartitionName on the Mac.

Your F:\Dir1\Dir2 would be /Volumes/PartitionName/Dir1/Dir2/ 'NIX does not remember the "current directory" on each volume, but you could save these in environment variables e.g.

CD_1=`pwd`

would save the current directory, which you can access with $CD_1

Another option is to create symlinks to directories of interest.

2

Unix systems make the separation between volumes as seamless as possible. Very few programs care on what volume (under what mount point) a file is. It's just irrelevant.

There are many facilities for “remembering” multiple directories. Look for facilities associated with directories, or with strings in general, not for facilities associated with multiple volumes: these facilities don't care whether on what volumes the directories are.

A very generic “remembering” facility is to set a variable. That variable can contain the path to a directory or any string you care to put there. On a Bourne-style shell (zsh, bash, ksh, etc.):

f=/media/usb1/dir1/dir2
d=~/dir3/dir4
cp $f/* $d/*

If the values contain spaces or \[*?, beware than on Bourne-style shells other than zsh, you need double quotes around the variable substitutions:

cp "$f"/* "$d"/*

You should always use double quotes in scripts, but on the command line, since you usually know what's in the variable, you can omit them unless you know that the variable contains spaces.

To quickly navigate between several directories, you can use the directory stack. Example:

cd /media/usr1/dir1/dir2
ls
pushd ~/dir3               # Remember /media/usr1/dir1/dir2 and go to ~/dir3
ls
cd dir4                    # Go to ~/dir3/dir4, /media/usr1/dir1/dir2 is still remembered
dirs                       # Show the directory stack
cp ~1/* .                  # Copy files from /media/usr1/dir1/dir2
popd +1                    # Forget /media/usr1/dir1/dir2
1

Unix and Windows both put files in certain places.

Programs file go in one place. data in another, usb drive data in another. Both systems support access to data/devices for the different uses.

The difference is exactly how.
In Windows you are given direct access to physical devices. In Unix/Linux the there is only one file system tree and then directories and other - multiple - devices are mapped to it. Thus /dir/x/y and /dir/x/z can be on totally different devices. In addition all raw devices such as hard drives and USB drives are also available, in the /dev directory.

Some 'translated' examples:

Windows             Linux                  Mac

c:\Program Files    /bin                   /bin
e:\                 /home                  /home
f:\ (usb)           /media/my-usb-drive    /Volumes/my-usb-drive

to answer your other questions - copying from 'c' to 'e' is just as simple in Linux - you cp (copy) from /some/location to /media/my-usb-drive

If you want to refer to the present working directory you can use $PWD and if you want to refer to a directory you were just in you can use $OLDPWD

You may need to learn more on:

  • Disk formats - what would work on both system*
  • Formatting a USB stick
  • Zipping up large files for transfer between systems

*look into NTFS - https://superuser.com/q/45130/95115

Your title mentions "a way to remember them".

You can use aliases: If you type

alias p='cd /bin'

or

alias programs='cd /bin'

then you can just type p [return] or programs [return] to go to that directory.

Also the last working directory is OLDPWD, e.g.

$ cd tmp
$ cd ~
$ echo $OLDPWD
/home/durrantm/tmp
2
  • You are in error /dev/hda1 is the raw device (not equivalent to e:). It would be equivalent to \\.\e:. And /dev/ttyUSB1 is a serial device, it can not be mapped to a file-system, equivalent to com7:, where 7 is the number of the day. Commented Jun 3, 2015 at 18:07
  • Yes you are correct. I was using guesses. Feel free to update the answer for more accuracy (or down-vote at your pleasure). Commented Jun 3, 2015 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .