May 08, 2014 Create Aliases on OS X and Linux for Faster Work on Command Line – Like You Can Configure the key c for clear command on Terminal or iTerm2. This sounds nice as we no longer will need to use tar -xzvf like bigger commands. We are using iTerm2 on our MacBook Pro running OS X 10.9, well the things are same on any.nix system.
Home > Articles > Apple > Operating Systems
␡- Shell Aliases
< BackPage 5 of 7Next >
This chapter is from the book UNIX for Mac OS X: Visual QuickPro Guide
This chapter is from the book
This chapter is from the book
Shell Aliases
Shell aliases are shortcut names for commands. Each alias consists of one word (or even one letter) that you can use instead of a longer command line. For example, you may find yourself using the command ls -F a lot. You can easily make a shortcut for that command: lf, for example. So when you use lf where the shell expects a command, then the shell will substitute ls -F.
To see all your current aliases:
- aliasThe alias command with no arguments displays all your current aliases. The first item on each line is the alias (which must always be a single string, with no spaces), and the rest of the line is the full command for which the alias is a shortcut. Figure 7.9 shows the default aliases for the tcsh shell. You can see that the alias l is a shortcut for the command ls -lg. The first word on each line is the name of the alias; the rest of the line is what gets executed when the alias is used.Several of the aliases are more complicated. The aliases in Figure 7.9 are for the tcsh shell, and several of them make use of specific advanced features of that shell (see man tcsh for all of the available features). For example, the alias in Figure 7.9 called line expects two arguments (indicated by !:1 and !:2), while the alias called ll takes all of its arguments (indicated by !*) and inserts them into the middle of a command line.Figure 7.9 Using the alias command to see all currently set aliases. Shown are the default aliases for the tcsh shell.
You can create aliases at the command line or by adding them to a configuration file.
Aliases created at the command line are only in effect for as long as you use that shell—that is, they disappear when you close that Terminal window. If you want an alias to always be available, you must put it in a con-figuration file.
To create an alias in tcsh or csh:
- alias lf 'ls -F'This will create an alias called lf, which the shell will translate into ls -F whenever you use lf as a command. Make sure to enclose the last argument in quotes, either single or double, so that everything after the alias name is treated as a single entity.Check to see that the alias is set:The lineshould be included in your aliases now.Tips
- If you want to have an alias use arguments from the command line inside the alias definition, you can use !:1 for the first argument, !:2 for the second, and so on. But you must escape the ! in the alias definition. So to define an alias called myword that takes its first argument and searches for it inside the file ~/mydictionary, you would use
- You could use that alias in this way:as a shortcut for
To create an alias in tcsh (or csh) that is set every time you start a shell:
- Open your ~/.tcshrc file (for the csh shell use ~/.cshrc).
- Add a line with the alias
- Save the file.
- Quit the editor.The new alias will be set for the next shell you start.
- Open a new Terminal window to check that the alias is set:You should see your new alias in the resulting list.TIPA set of example aliases for the tcsh shell are contained in the file /usr/share/tcsh/examples/aliases.
To create an alias in bash:
- alias lf='ls -F'Note that there are no spaces before or after the equal sign.
- aliasThe shell shows all your current aliases, including the one you just created. As with the tcsh shell, bash aliases created at the command line will disappear when you exit the shell.
To create an alias in bash that is set every time you start a shell:
- Open your ~/.bash_profile file.
- Add a line with the alias—for example, alias lf='ls -F'
- Save the file.
- Quit the editor.The new alias will be set for the next shell you start.
- Open a new Terminal window to check that the alias is set:You should see your new alias in the list:
Shell functions
Unlike aliases in the tcsh shell, aliases in bash cannot have command-line arguments included in them. However, bash allows you to create shell functions, which can make use of their arguments.
The term shell function applies to series of shell command lines. This is similar to an alias, except that a shell function can be many lines long, and you may use the special variables $1 for the first argument, $2 for the second, and so on.
Shell functions should be defined in your ~/.bash_profile.
To create a shell function in bash:
- Open your ~/.bash_profile.The entire function you will be entering is shown in Figure 7.10. This sample function looks up a word in two different files that make up a dictionary.Figure 7.10 Code listing of a bash shell function.
- Enter the first line of the new function. In this example you are creating a function called 'word':The parentheses tell bash that this is a function definition. The bracket ({) marks the beginning of the commands in the function.
- Enter the body of the function:Notice that the function can have more than one line of commands.The $1 is a variable that will be replaced with the first argument when you use the function in a command line. (Read the file /usr/share/dict/README for a description of the web2 and web2a files.)
- Finish the function definition with a }. Double-check that what you entered looks like Figure 7.10.
- Save the file.
- Quit the editor.The new function will be in effect with the next Terminal window you open.
- Open a new Terminal window.
- Test the function by trying it on the command line. If you are using the example function from Figure 7.10, then the first argument you supply is used in the function. The function searches two different files for its first argument.
- word auspicYou should get the output shown in Figure 7.11. Your new shell function, word, takes its first argument (the $1 in the function) and searches for it in the two files. The function is really a short shell script (see Chapter 9, 'Creating and Using Scripts') but is part of your personal shell configuration.Figure 7.11 Using the new shell function to look up 'auspic' in the dictionary.
Related Resources
- Book $31.99
- eBook (Watermarked) $38.39
- eBook (Watermarked) $41.59
Type code | alis |
---|---|
Uniform Type Identifier (UTI) | com.apple.alias-file |
Magic number | 'book0000mark0000' |
Developed by | Apple, Inc. |
Type of format | shortcut |
In classic Mac OSSystem 7 and later, and in macOS, an alias is a small file that represents another object in a local, remote, or removable[1]file system and provides a dynamic link to it; the target object may be moved or renamed, and the alias will still link to it (unless the original file is recreated; such an alias is ambiguous and how it is resolved depends on the version of macOS). In Windows, a 'shortcut', a file with a .lnk extension, performs a similar function.
It is similar to the Unixsymbolic link, but with the distinction of working even if the target file moves to another location on the same disk (in this case it acts like a hard link, but the source and target of the link may be on different filesystems, and the target of the link may be a directory). As a descendant of BSD, macOS supports Unix symbolic (and hard) links as well.
Function[edit]
An alias acts as a stand-in for any object in the file system, such as a document, an application, a folder, a hard disk, a network share or removable medium or a printer. When double-clicked, the computer will act the same way as if the original file had been double-clicked. Likewise, choosing an alias file from within a 'File Open' dialog box would open the original file. The purpose of an alias is to assist the user in managing large numbers of files by providing alternative ways to access them without having to copy the files themselves. While a typical alias under the classic Mac OS was small, between 1 and 5 KB, under macOS it can be fairly large, more than 5000 KB for the alias to a folder.
Preventing alias failure[edit]
An alias is a dynamic reference to an object. The original may be moved to another place within the same filesystem, without breaking the link. The operating system stores several pieces of information about the original in the resource fork of the alias file. Examples of the information used to locate the original are:
- path
- file ID (inode number)
- directory ID (inode number)
- name
- file size
Since any of these properties can change without the computer's knowledge, as a result of user activity, various search algorithms are used to find the most plausible target. This fault-tolerance sets the alias apart from similar functions in some other operating systems, such as the Unixsymbolic link or the Microsoft Windowsshortcut, at the expense of increased complexity and unpredictability. For example, an application can be moved from one directory to another within the same filesystem, but an existing alias would still launch the same application when double-clicked.
The question can arise of how an alias should work if a file is moved, and then a file is created with the same name as the original moved file, since the alias can be used to locate both the original name and the new location of the original file. With symbolic links the reference is unambiguous (soft links refer to the new file, hard links to the original). Before Mac OS X 10.2, however, such an ambiguous alias would consistently find the original moved file, rather than the recreated file. In Mac OS X 10.2 and later releases, the new file is found, matching the behaviour of symbolic links [1]. macOS applications can programmatically use the old behavior if required.
Aliases are similar in operation to shadows in the graphical Workplace Shell of the OS/2 operating system.
Distinguishing marks[edit]
In System 7 through Mac OS 9, aliases distinguished themselves visually to the user by the fact that their file names were in italics. To accommodate languages that don't have italics (such as Japanese), in Mac OS 8.5 another distinguishing mark was added, badging with an 'alias arrow'—a black arrow with a small white border—similar to that used for shortcuts in Microsoft Windows.
In macOS, the filenames of aliases are not italicized, but the arrow badge remains.
File structure[edit]
Dropbox Alias Mac Os X
The alias files in macOS start by the magic number
62 6F 6F 6B 00 00 00 00 6D 61 72 6B 00 00 00 00
which is in ASCIIbook␀␀␀␀mark␀␀␀␀
(␀ representing the Null character).Following the magic number, it has been reported that an alias has a set of records inside it, each record is 150 bytes long and consists of the fields shown below (all integers are big endian).[2] However, alias files are far larger than this would explain, and include other information at least including icons.[3][4]
- 4 bytes user type name/app creator code = long ASCII text string (none = 0)
- 2 bytes record size = short unsigned total length
- 2 bytes record version = short integer version (current version = 2)
- 2 bytes alias kind = short integer value (file = 0; directory = 1)
- 1 byte volume name string length = byte unsigned length
- 27 bytes volume name string (if volume name string < 27 chars then pad with zeros)
- 4 bytes volume created mac date = long unsigned value in seconds since beginning 1904 to 2040
- 2 bytes volume signature = short unsigned HFS value
- 2 bytes volume type = short integer mac os value (types are Fixed HD = 0; Network Disk = 1; 400kB FD = 2;800kB FD = 3; 1.4MB FD = 4; Other Ejectable Media = 5 )
- 4 bytes parent directory id = long unsigned HFS value
- 1 bytes file name string length = byte unsigned length
- 63 bytes file name string (if file name string < 63 chars then pad with zeros)
- 4 bytes file number = long unsigned HFS value
- 4 bytes file created mac date = long unsigned value in seconds since beginning 1904 to 2040
- 4 bytes file type name = long ASCII text string
- 4 bytes file creator name = long ASCII text string
- 2 bytes nlvl From (directories from alias thru to root) = short integer range
- 2 bytes nlvl To (directories from root thru to source) = short integer range (if alias on different volume then set above to -1)
- 4 bytes volume attributes = long hex flags
- 2 bytes volume file system id = short integer HFS value
- 10 bytes reserved = 80-bit value set to zero
- 4+ bytes optional extra data strings = short integer type + short unsigned string length (types are Extended Info End = -1; Directory Name = 0; Directory IDs = 1; Absolute Path = 2; AppleShare Zone Name = 3; AppleShare Server Name = 4; AppleShare User Name = 5; Driver Name = 6; Revised AppleShare info = 9; AppleRemoteAccess dialup info = 10)
- string data = hex dump
- odd lengths have a 1 byte odd string length pad = byte value set to zero
Alias record structure outside of size length[edit]
The following is for use with the Apple's Alias Resource Manager.
- 4 bytes resource type name = long ASCII text string
- 2 bytes resource ID = short integer value
- 2 bytes resource end pad = short value set to zero
Java code to flag an alias file
Managing aliases[edit]
User interface[edit]
In System 7, the only way to create an alias was to select the original and choose 'Make Alias' from the 'File' menu. An alias, with the same name and ' alias' appended would then be created in the same folder. In later versions, it became possible to create aliases by drag-and-drop, while holding down the command and option modifier keys.
Mac OS 8.5 added a feature for re-connecting aliases that had been broken for one reason or another (when the simple search algorithms failed to find a reliable replacement). This was done by selecting a new target through the standard Open File dialog.
In Mac OS 8.5 options were added for command-optiondragging an object in the Finder to create an alias at that location. This is where the alias cursor was added to the system. The cursor mirrors the appearance of the 'create shortcut' cursor on Windows systems.
Programming API[edit]
The Alias Manager API is part of Carbon. It is unknown whether it was present in Mac OS Classic.[5]
Mac OS X 10.6 introduced some alias-related APIs to Cocoa, as a part of
NSURL
.[6]Relation to BSD symbolic and hard links[edit]
Unix and similar operating systems provide 2 features very similar to macOS aliases: symbolic links and hard links. When using the macOS Finder, links are displayed and treated largely like macOS aliases, and even carry an identical 'Kind' attribute. However, when using the shell command line, macOS aliases are not recognized: for example, you cannot use the cd command with the name of an alias file. This is because an alias is implemented as a file on the disk that must be interpreted by an API while links are implemented within the filesystem and are thus functional at any level of the OS.
There is currently no pre-installed command to resolve an alias to the path of the file or directory it refers to. However, a freely available C program makes use of the Mac Carbon APIs to perform this task.[7] Given that, commands such as cd can be set up to check for aliases and treat them just like symbolic or hard links.
References[edit]
- ^Files: Chapter 4 - Alias Manager; Search Strategies — Inside Macintosh developer documentation
- ^Some information about MacOS aliases collected from the web. (reverse engineering effort)
- ^Forum discussion of the large size of aliases.
- ^'Further details, including changes with various Mac OS versions'. Archived from the original on 2013-04-30. Retrieved 2018-10-11.
- ^'Alias Manager'. Apple Developer Documentation.
- ^https://github.com/nathanday/ndalias/issues/3
- ^Davis, Thos. 'getTrueName.c'. Mac OS X Hints. IDG. Retrieved 24 October 2016.
External links[edit]
- System 7 aliases — Article about System 7 aliases, from 1992
Mac Remove Alias
Best Alias For Mac Os X
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Alias_(Mac_OS)&oldid=961468240'