Where does Cygwin stores Unix-specific file attributes, e.g. the executable flag? Is it possible to read them with non-Cygwin applications?
The original file attributes are being translated into unix file attributes depending on the oriinal filesystem. Have a look at this explanation: http://cygwin.com/cygwin-ug-net/using-filemodes.html
Related
Why is bash(.sh) script not executable by default.
I agree that while we touch any file in linux it is created for reading purpose.
But since file name extensions such as sh and csh are for execution purpose.
Won't it be ideal to touch them in an executable mode.
Question might sound redundant to, but i still thought of asking it :)
Ultimately the answer to this question is that that's not what touch was designed to do.
In fact, touch was not even designed to create files; its primary purpose in life is to change file timestamps. It is merely a side effect of that purpose, and the fact that the designers decided to be a little more generous and create the target file if it doesn't already exist (and if you didn't provide the -c option), that it allows you to create files at all.
It should also be mentioned that there are other techniques that create files, such as redirection (echo 'echo '\''I am a script.'\'';' >|script.sh;). The act of file creation is a generic one, and the whole concept of a file is a generic one. A file is just a byte stream; what goes in the byte stream is unspecified at the file abstraction layer. As #AdamGent mentioned, Windows requires certain types of executable files to have certain extensions in order to be executed properly, but even in Windows, you can put executable code in non-executable-extensioned files, and you can put non-executable content in executable-extensioned files. There's no enforcement of file name / file content correspondence at the file layer.
All of that being said, it would often be a convenience if you could easily create a script in Unix that automatically has executable permission set. I've actually written a script to allow me to edit a new file in vim, and then have its permissions set to executable after write-quitting. The reason this potential convenience has not been standardized into a utility is likely related to the concern about security; you don't want people to accidentally make files executable, because that raises the risk of security holes.
You can always write your own program to create a file and make it executable, perhaps based on the extension of the file name.
Another thing that can be added here is that even shell scripts don't always need to be executable. For example, if you write a script that is only intended to be sourced from existing shell processes (via the source or classic . builtins), then the script does not need to be executable at all. Thus, there are cases where the file extension itself does not provide enough information to determine what the appropriate permissions are for the file.
There is nothing in the file name that says a file is even a script. Common practice perhaps says that .sh and .csh are scripts but there is no such rule.
What makes a file an executable script is the magic number in the beginning of the file. The magic number #! (the shebang, which has many other names as well) means the file is a script. For example
#!/bin/bash
When you try to execute the file (it must then also be set to executable in the permissions), the Linux kernel looks at the first two bytes of the file and finds the magic number (#!). It then knows it is supposed to execute whatever comes after the Shebang with the name of the file as argument followed by the rest of the arguments passed.
If you type the following in the shell
blablabla.sh lol1 lol2
The shell recognizes that you are trying to execute something so it invokes the kernel
exec blablabla.sh lol1 lol2
The kernel finds the shebang, and it becomes
exec /bin/bash blablabla.sh lol1 lol2
By exec i mean one of the exec family system calls.
Other fun names for #! include sha-bang, hashbang, pound-bang, hash-exclam and hash-pling
Because the .sh script extension is completely arbitrary in Unix environments. Its only a convention. You can name your script whatever you like so long as it has an executable bit set. This is unlike Windows where I believe its required (.com, .exe, and I think .dll).
touch just changes the timestamp of the file. It again does not care what the file extension of the file is. In fact most tools in Unix do not care about file extension.
I have a piece of metadata that I would like to hide in a plain text file, won't be shown by generic text editor, is there a way to do that?
Not within the file itself. For storing metadata I suggest using alternative data streams in the filesystem. In OS X (with HFS) they're called Forks and in Windows (with NTFS) they're called Alternate Data Streams. You're probably already familiar with the "Resource fork" on OS X.
...basically it's a hidden file with the same name as the original file (and moves with it), except it's accessed with special (normally illegal) characters in the file path.
Note that ADS files are not moved/copied between incompatible filesystems (e.g. from NTFS to FAT), when compressed into a zip or archived into a tarball, nor when uploaded, so never assume that your ADS file will be persistent (making it useful for storing local-machine-only metadata, such as editor preferences, etc)
On Windows, you can learn more here: http://support.microsoft.com/kb/105763
On Mac OS X, you can learn more here: https://developer.apple.com/library/mac/documentation/Carbon/Reference/File_Manager/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/c/func/FSAllocateFork
On Linux, the main filesystems (extfs: ext3, ext4, etc) do not support forks in the filesystem. When reading a HFS volume on Linux you access forks by appending the fork name to the filename (e.g. "/foo/bar/rsrc" to access the resource fork of the file "/foo/bar").
I would like to convert a file to .dat below is my query
I have a File eg: ABC this file doesn't have an extension(when i click its propertise it says type of file: file ) I want to convert this file to a .dat by writing a unix script
Linux (and Unix) do not use the file extension to define the type of a file, though some programs to use the file extension as a guideline. Unix/Linux examines the file magic number (the first bytes) of the file to determine the file type, though the program 'file' is the best explanation of how this is done (three tests, filesystem tests, magic tests, and language tests, the first that succeeds determines the file type).
Windows makes heavy use of the file extension to determine file type, and keeps metadata which maps file extension to application(s) which understand the file.
Linux/Unix uses the file magic number, examination of the first line of the file, and hints at the file type (for human use and some program use) using the file extension.
MacOS tracks file metadata using extension, file type code and creator code (metadata kept apart from filename), although OSX is derived from a Unix-like OS, so many of the Linux/Unix notes are applicable.
I use various 3rd party libraries to convert files on my Linux server. For instance, ImageMagick/convert for images, libreoffice3.5/convert-to for Microsoft Office documents, etc.
Is it possible that these applications require the pre-converted file to have the proper extension for the type of file? For instance, if the file was a png file, it would need to be called whatever.png and not just whatever.
Thank you
your question sounds general, and in general linux apps do not require extensions. bash will execute a .png file with shell commands happily and vi will open a text file called a.exe. extensions are in general not a unix/linux concept to begin with and . is just an allowed character in the file name.
this being said, some particular application may interpret or even require correct extensions.
I am trying to find a magic header collection of file types which can be feeded to file command using file -m but no avail.
Distro pre included file heaers are not too many to detect all the file types on the internet..
Has anyone come up with any collection , which is more than default list?
Thanks!
FILE SIGNATURES TABLE from Gary Kessler is usually a good reference.
According to him, this page provides a copy of a magic file recovered from a Red Hat Linux system.