Linux directory permission s - bash

My website runs on a third party server, and I encountered directories with the following permissions:
folder1 drwsr-s---
folder2 drwxr-s---
folder3 drwxr-x---
I'm familiar with the d, the r, the w, the x (for folders), and the -. While editing (in Filezilla), it seems all of these are equal to chmod 750.
man chmod tells me that s stands for "set user or group ID on execution". What does that mean? And how come it maps onto the same chmod code? Should I worry about this on the server?

The 's' bits are referred to as the "setuid" and "setgid" bits. What it does depends on the file type.
On a directory, as in your example, these bits set the default user or group for all files created in the directory.
EG, if you have a directory owned by foo:foo, with the setuid and setgid bits set, then all files created in that directory will be owned by foo:foo, regardless of who creates them.
In your example, the "setgid" bit is set for each directory. This means that for every file created in these directories, the owner will be the user who created the file, but the group will be set to match the directory's group, rather than the user's main group.

Related

Laravel rename a directory using Storage facade

I'd like to rename a directory with the Storage Facade (S3 object). I've tried using move and rename, both with and without trailing slash, none of these work.
Storage::disk('s3')->move($dir_temp, $dir_final);
Storage::disk('s3')->move($dir_temp.'/', $dir_final.'/');
Storage::disk('s3')->rename($dir_temp, $dir_final);
Storage::disk('s3')->rename($dir_temp.'/', $dir_final.'/');
I did use rename function, and it did work for renaming a directory.
This code worked for renaming directory dir 1 to dir 2:
Storage::disk('movies')->rename('dir 1', 'dir 2');
Now, it depends on what error did you get.
make sure, you have rwx rights to the parent directory
make sure the source directory exists, and the destination does not
make sure the name is valid for a name, for example, windows OS will not allow certain characters in the name
make sure you do have permissions to the source directory for that operation
be very carefull with slashes, as windows OS has different path slashes than linux.
For the directory separator, to be platform independent, you can use
$DS = DIRECTORY_SEPARATOR;
Also, the permissions can be quite tricky, as, if you have the app deployed, traditionally, the http user is the user under which all the code is executed, so keep that in mind.

Go Lang ioutil.writeFile function making directories and files read only

I have created a go program to trace over all the files under a root path and replace a specific source string with a target string after that writhing the updated content into the existing file and for that, I have used filepath.Walk function to trace over the files and ioutil.ReadFile and ioutil.WriteFile to read from and write into the file.
So, the problem is ioutil.WriteFile function is marking the root directory, subdirectories, and files as read-only. I checked online for the solution of this but could not find anything appropriate.
Below is the code snippet of same.
if strings.Contains(data, sourceString) {
data = strings.ReplaceAll(data, sourceString, targetStringArray[index])
ioutil.WriteFile(path, []byte(data), os.FileMode(0655))
}
File mode 0655 is translated to permissions rw- r-x r-x. This combination of permissions is weird, because there is no "x" for the owner, but there are for the group and others. Maybe what you want is 0755 (rwx r-x r-x) for executable files or 0644 (rw- r-- r--) for non-executable files, which are the most used ones.

Is there a difference between C: and C:\ in Windows 7?

I am writing a spreadsheet that has paths to files.
It will be used in a custom script with some not-well-known software that the company I work at uses. The custom script will read the column in the spreadsheet that has the path + file, in order to find a file residing at that location.
The spreadsheet has path+file entries such as
C:3.jpg
but, usually, in real life, I see a path+file written like this:
C:\3.jpg
Does it matter which way it is written?
Yes. The first means 3.jpg in whatever the current directory is on drive C, while the second means 3.jpg in the root directory of drive C.
In other words, if you're at a command prompt:
C:\Temp>
then C:3.jpg would refer to C:\Temp\3.jpg, while at the prompt
C:\Users\silph>
referring to C:3.jpg would be C:\Users\silph\3.jpg.
On the other hand, at the same two prompts (C:\Temp> and C:\Users\silph>), referring to C:\3.jpg would always refer to the same 3.jpg located in the root of drive C:.

Bash/shell/OS interpretation of . and .. — can I define ...?

How do . and .., as paths (vs. ranges, e.g., {1..10}, which I'm not concerned with), really work? I know what they do, and use them all the time, but don't fully grasp how/where they're interpreted. Does the shell handle them? The interpreting process? The OS?
The reason why I'm asking is that I'd like to be able to use ... to refer to ../.., .... to refer to ../../.., etc. (up to some small finite number; I don't need bash to process an arbitrarily large number of dots). I.e., if my current directory is /tmp/let/me/out, and I call cd ..., my resulting current directory should be /tmp/let. I don't particularly care if ... etc. show up in ls -a output like . and .. do, but I would like to be able to call cat /tmp/let/me/out/..../phew.txt to print the contents of /tmp/phew.txt.
Pointers to relevant documentation appreciated as well as direct answers. This kind of syntax question is very hard to Google.
I'm using bash 4.3.42, by the way, with the autocd and globstar shell options.
. and .. are genuine directory names. They are not "sort-cuts", aliases, or anything fake.
They happen to point to the same inode as the other name you use. A file or directory can have several names pointing to the same inode, these are usually known as hard links, to distinguish them from symbolic (or soft) links.
If you are on Linux or OS X you can use stat to look at most of the inode metadata - it is what ls looks at. You will see there is an inode number. If you stat . and stat current-directory-name you will see that number is the same.
The one thing that is not held in the inode is the filename - that is held in the directory.
So . and .. reside in the directory on the file system, they are not a figment of the shell's imagination. So, for example, I can use . and .. quite happily from C.
I doubt you can change them - personally I have never tried and I never will. You would have to change what these filenames linked to by editing the directory. If you managed it you would probably do irreparable damage to your file system.
I write this to clarify what has already been written before.
In many file systems a DIRECTORY is a file; a special type of file that the file system identifies as being distinctly a directly.
A directory file contains a list of names that map to files on the disk
A file, including a directly does not have an intrinsic name associated with it (not true in all file systems). The name of a file exists only in a directory.
The same file can have an entry in multiple directories (hard link). The same file can then have multiple names and multiple paths.
The file system maintains in every directory entries for "." and ".."
In such file systems there are always directory ENTRIES for the NAMES "." and "..". These entries are maintained by the file system.
The name "." links to its own directory.
The name ".." links to the parent directory EXCEPT for the top level directory where it links to itself (. and .. thus link to the same directory file).
So when you use "." and ".." as in /dir1/dir2/../dir3/./dir4/whatever,
"." and ".." are processed in the exact same way as "dir1" and "dir2".
This translation is done by the file system; not the shell.
cd ...
Does not work because there is no entry for "..." (at least not normally).
You can create a directory called "..." if you want.
You can actually achieve something like this, though this is an ugly hack:
You can run a command before every command entered to bash, and after every command. For that you trap the DEBUG pseudo signal and set a command to PROMPT_COMMAND, respectively.
trap 'ln -s ../.. ... &>/dev/null | true' DEBUG
PROMPT_COMMAND='rm ...'
With this, it seems like there's an additional entry in the current directory:
pwd
# /tmp/crazy-stuff
ls -a
# . .. ... foo
ls -a .../tmp/crazy-stuff
# . .. ... foo
Though this only works in the current directory, because the symbolic links is deleted after each command invokation. Thus ls foo/bar/... won't work this way.
Another ugly hack would be to "override" mkdir such that it populates every new directory with these symbolic links.
See also the comments on the second answer here, particularly Eliah's: https://askubuntu.com/questions/327126/what-is-a-dot-only-named-folder
Much in the same way that when you cd into some directory subdir, you're actually following a pointer that points to that directory, .. is a pointer added by the OS that points to the parent directory, and I'd imagine . works the same way.

Ruby (Errno::EACCES) on File.delete

I am trying to delete some XML files after I have finished using them and one of them is giving me this error:
'delete': Permission denied - monthly-builds.xml (Errno::EACCES)
Ruby is claiming that the file is write protected but I set the permissions before I try to delete it.
This is what I am trying to do:
#collect the xml files from the current directory
filenames = Dir.glob("*.xml")
#do stuff to the XML files
finalXML = process_xml_files( filenames )
#clean up directory
filenames.each do |filename|
File.chmod(777, filename) # Full permissions
File.delete(filename)
end
Any ideas?
This:
File.chmod(777, filename)
doesn't do what you think it does. From the fine manual:
Changes permission bits on the named file(s) to the bit pattern represented by mode_int.
Emphasis mine. File modes are generally specified in octal as that nicely separates the bits into the three Unix permission groups (owner, group, other):
File.chmod(0777, filename)
So you're not actually setting the file to full access, you're setting the permission bits to 01411 which comes out like this:
-r----x--t
rather than the
-rwxrwxrwx
that you're expecting. Notice that your (decimal) 777 permission bitmap has removed write permission.
Also, deleting a file requires write access to the directory that the file is in (on Unixish systems at least) so check the permissions on the directory.
And finally, you might want to check the return value from File.chmod:
[...] Returns the number of files processed.
Just because you call doesn't mean that it will succeed.
You may not have access to run chmod. You must own the file to change its permissions.
The file may also be locked by nature of being open in another application. If you're viewing the file in, say, a text editor, you might not be able to delete it.
In my case it was because the file I had been trying to delete--kindle .sdr record--was directory, not file. I need to use this instead:
FileUtils.rm_rf(dirname)

Resources