All files made by root is write-protected - linux

Is it normal that all files and folders, which is made by root, is write-protected?
For instance, take a look at terminal log below:
shine#Shine-Ubuntu:~$ sudo -s
root#Shine-Ubuntu:~# echo >> 1.txt
root#Shine-Ubuntu:~# exit
exit
shine#Shine-Ubuntu:~$ rm 1.txt
rm: remove write-protected regular file '1.txt'?
(As you might know, echo >> FILE_NAME creates a file and names it FILE_NAME.)
When a file is made by root, and i want to delete it via user, it says remove write-protected regular file 'FILE_NAME'?
What should i do so the files and folders made by root won't be write-protected?

UMASK is the default permissions given when a new file or folder is created on a Linux machine. Detailed documentation/usage of umask can be seen here. Check umask setting on your system

Related

How can I execute a command anywhere if certain required files are different directories?

Let say the command be my_command
And this command has to be prepared specific files (file1, file2, and file3) in the current working directory.
Because I often use my_command in many different directories, I'd like to keep the certain files in a certain directory and execute my_command without those three files in the working directory.
I mean I don't want to copy those three files to every working directory.
For example:
Directory containing the three files /home/chest
Working directory: /home/wd
If I execute command my_command, it automatically recognizes the three files in /home/chest/
I've thought the way is similar to add $PATH and not the executable files but just files.
It seems like the files needs to be in the current working directory for the vasp_std command to work as expected, I am thinking that you could simply add all files in a include folder in you home directory and then create a symbolic link to this folder from your script. In the end of your script the symbolic link will then be deleted:
#!/bin/bash
# create a symbolic link to our resource folder
ln -s ~/include src
# execute other commands here
# finally remove the symbolic link from the current directory
unlink src
If the vasp_std command require that the files are placed directly under the current working directory you could instead create a symbolic link for each file:
#!/bin/bash
# create link for to all resource files
for file in ~/include/*
do
ln -s $file `basename $file`
done
# execute other commands here
# remove any previously created links
for file in ~/include/*
do
unlink `basename $file`
done

Why does the filesystem not recognize a file named ../blah.sh

I am using Ubuntu 16.04.3 LTS with ext4 filesystem.
When I make a file like:
touch ../blah.sh
It does not show up when I use:
ls -al
When I try to delete the file with:
rm * --> : rm: cannot remove '*': No such file or directory.
However when I delete it with:
rm ../blah.sh ---> it succeeds.
Besides this I am able to edit the file with vim, put code in there and then run it like:
./../blah.sh
How is this behavior possible? Is this bash specific behaviour or from the operating system? and is it possible to hide a file like this?
When you use .. you are creating the file in the parent directory of where you currently are.
If you try this it should show the file:
ls -al ..
Also for rm you'd have to do the following, though it's dangerous obviously.
rm ../*

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

How to delete all files in one particular directory

In Linux, how can I delete all files in particular directory? For example /home/xd/karthik is my path; I want to delete all files in the above directory, if the disk usage exceeds 90%. How can I write a script for that?
rm /path/to/directory/*
add rm -r to remove the file hierarchy rooted in each file argument.
dont need script just basic shell command

Linux : How to delete locked file

I want to remove xyz_DB.lock.db file. I tried as root but couldn't delete it. How to remove it in terminal. My initial requirement was remove a folder. but it includes this locked file. And is there anyway to delete folder directly which include a locked file ?
Check with lsattr command if the immutable bit is set for the file, it will show (i)
# lsattr file
----i--------e- file
If so, change it using following command:
# chattr -i file
And then try to remove it.
Try either changing the files permissions through the GUI or use rm -rf on the directory that contains it.
try "chown" to provide permission to your file/folder and then delete it,
e.g: assume username= amol and filename=myfile.txt,,
For File:--- sudo chown amol:amol myfile.txt
For Folder:-- sudo chown -R amol:amol directory_name

Resources