How to create Subdirectories using Windows Command Line? - directory

How would I create one directory with four sub-directories? I'm having trouble with this question for a lab I'm doign.
What commands would you issue to build the following directory
structure on the hard drive: Create the directory PROGRAMS in the
root. In the PROGRAMS directory create four subdirectories called:
Adobe, Email, Graphics and MSOffice. In the first three subdirectories
create two user directories called: Personal and Guest. In the
MSOffice subdirectory create two subdirectories called: Word and
Excel. Include a Tree view drawing or printout of the directory
structure.
I know that to create a directory, you'd use
mkdir \programs
But I'm not sure that's even how you start.

Start with the outermost directory. (md and makedir are the same thing, and both works on Windows.)
md \Programs
Now create the next level (repeating for each one):
md \Programs\Adobe
Now the next level:
md \Programs\Adobe\Guest
Continue as needed.

Related

Manipulating multiple files with same name

I am trying to move about 1000 files that all begin with "simulation." into one directory entitled "simulations." I am using a remote server, and the files are currently in my home directory on the server. I need to move them to a separate directory because I need to, ultimately, append all the "simulation." files into one file. Is there a way to either append only the files in my home directory that begin with "simulation." or move only these files into a new directory?
Thank you.
Assuming you can change directories to the desired path on the remote server... and the simulations are located in /currentPath ... then....
cd desiredPath
mkdir simulations
mv /currentPath/simulation* simulations
(to futher answer your question... if you wanted to append all the files together, you could type cat simulation* > allSimulations.txt

Where should I set the variable PATH in R?

I constantly need to call Tex Live binaries for compilation in R. However after the upgrade of Tex Live distribution, the path to current binaries needed to updated manually in the PATH(Sys.getenv("PATH")) variable.
As a single user on a Ubuntu system, which file should I update the value in, so that R gets the PATH correctly irrespective of whichever directory R is launched from.
One point I still don't gather is from where does R gets its site-wide (I mean for all users, even if faulty in saying so) PATH variable set, because no such variable name as "PATH" occur inside any files (Renviron, Renviron.site, Rprofile.site) in either of "R_HOME/etc/" and user's home directory? I also haven't set Sys.getenv("R_ENVIRON") and Sys.getenv("R_ENVIRON_USER") values.
I'd appreciate anybody's input here.
#JeffreyGoldberg's solution was close, but not quite right.
Rprofile files are interpreted as R code
Renviron files can only contain name value pairs, and are not interpreted as R code
From the help for Startup:
Note that there are two sorts of files used in startup: environment files which contain lists of environment variables to be set, and profile files which contain R code.
I'm not sure if this question is asking specifically how one can set the site wide value of PATH, rather than PATH for one specific user, but there are three locations you can put these files.
A project directory (i.e., a directory you choose to launch R from)
HOME
R_HOME/etc
These locations are searched in the order numbered above. The first location can contain configurations specific to a project, the second contains those specific to a user, and the third, site wide configuration settings. When a file is found it is used, so local takes precedence over global. Don't think you can create a more specific version that simply updates what you've done in a more general configuration file. R_HOME/etc/Renviron is created on installation and should not be edited. You may create a file called R_HOME/etc/Renviron.site, but do not edit R_HOME/etc/Renviron.
To create a site wide value of PATH, you will want to set it in a file in R_HOME/etc. Here you can use either Renviron.site or Rprofile.site for the file name. For a file in R_HOME/etc, Do not use Renviron, Rprofile, .Renviron, or .Rprofile for the name of a profile or environment file in this location. You can find out what R_HOME is in an R session using R.home(), or Sys.getenv("R_HOME")
To create a PATH value for a single user, set it in a file in HOME, which you can find in your R session using Sys.getenv("HOME") or path.expand("~"). You can also just use "~" to refer to HOME. Here, an Renviron file should be ~/.Renvironand an Rprofile file ~/.Rprofile. Take note of the difference between how profile and environment files are named in your HOME directory vs. R_HOME/etc
To create a PATH for a single project, set it in a file in that project's top level directory. Name the files as you would in your home directory (.Rprofile or .Renviron).
If you are creating an Renviron file, the file should include the following line:
PATH=<your path>
< and > should not be included. An example would be:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
If you are creating an Rprofile file, the file should include the following line:
Sys.setenv("<your path>")
again, don't include "<" or ">". An example would be:
Sys.setenv("/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin")
There are various ways of doing this that get and edit a PATH variable (e.g., tack on a new path at the end, or the beginning). You can also use the strategy of setting an environment variable if it doesn't already exist and/or doesn't contain something you want it to. I've come to prefer just setting up my path simply, and coding it directly.
One final note, if you run R from a command line interface, environment variables may be inherited from your shell. RStudio also has its own startup sequence and may modify the end of your PATH variable. It should start as it is defined in your Rprofile or Renviron files. The R Console app itself has the fewest quirks with system environment variables, and should accept your path exactly as it is set with an Rprofile or Renviron file.
Edit: I should have tested before posting. What I describe below did not work. (Down voting my own answer is a strange thing.)
On my system (macOS, bash), R.app is not picking up my $PATH from my shell environment or .profile. However RStudio is picking it up. I do not understand the different behaviors.
One way to get consistent behavior would be to specify this in an Renviron file.
If you create a file named .Renviron in your come directory with a line like
Sys.setenv(PATH="/opt/local/bin:usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin")
(but of course with the path elements you need) that should give you consistent behavior.
The downside is that you need to manually maintain this. I suppose you could run a script from one of your other start up scripts that generated the .Renviron file. But either way, I consider this whole thing a work around in place of actually understanding where R picks up its environment from.

Add existing scripts to an Rstudio project

I'm working in Rstudio and have multiple scripts open that have different working directories; however, each working directory exists within a larger folder on my computer (see below). Is it possible to add these scripts to an Rstudio Project without reorganizing all my files and changing each script's working directory?
File structure on computer:
Folder A
~~Folder 1
~~Folder 2
~~Folder 3
Say I have 3 scripts open, each with a working directory of either Folder 1, 2, or 3. Can I create a project that incorporates all three scripts. Say, set working directory to "Folder A"
Thanks much.
Technically, you can change working directory programmatically within a project, but this is considered a very poor practice and is strongly recommended against. However, you can set working directory at a project's top level (full path to Folder A, in your example) and then refer to scripts and objects, located in Folders 1-3 via corresponding relative paths. For example: "./Folder1/MyScript.R" or "./Folder2/MyData.csv".
It should be possible to create a project in the larger folder. You could even construct a simple master script in Folder A to manage this workflow:
setwd("./Folder 1")
source("scriptx")
setwd("..")
setwd("./Folder 2")
source("scripty")
setwd("..")
setwd("./Folder 3")
source("scriptz")
setwd("..")
Compared to source("Folder 1/scriptx") which runs each script within Folder A the master script would be running each script within it's own folder. Just make sure to use setwd("..") after running code in each folder and you can even run code in between to save output to the main Folder A.
If your workflow always creates folders in this manner I don't see how this would not be reproducible if you used relative paths. Albeit platform dependent, this modified version would create folders on the fly and run scripts kept in Folder A.
system("mkdir Folder_1")
setwd("./Folder_1")
source("../Folder A/scriptx")
setwd("..")
Notice here that when running terminal commands in R, it is recommended avoid spaces in directory or file names.

changing home directory in R

In R, if I use the command
write.csv(res,"~/table1_n500.csv")
, then the result is saved in C:\Users\John Smith\Documents.
But I expected it to be saved in C:\Users\John Smith\.
Can I change this home directory (referred by ~ mark) to C:\Users\John Smith\?
From my personal experience, i usually import data from a file (for example in the directory C:\Users\John Smith\DATA)
then i will set working directory as
setwd("C:/Users/John Smith/DATA")
While i want to save the output file in other directory like "C:\Users\John Smith" but not in the data folder.
so i will set relative working directory like
setwd("../")
And when you type getwd()
you will get [1] "C:/Users/John Smith"
Wish this help.
There are two ways to deal with this problem.
1.) Use the function setwd() to set the working directory (or home directory). All save and read commands will look for files in that working directory. I use this only sparingly, and for quick tiny project.
2.) A more preferred approach is to define a variable like dataInputDir, and use function file.path(dataInputDir, <your filename>) to generate a file path. The advantage is that if you are reading (writing) data from (to) multiple directories, you can do this more efficiently:
file.path(dataInputDir1, <your file or dir name>)
file.path(dataInputDir2, <your file or dir name>)
file.path(dataOutputDir1, <your file or dir name>)
file.path(dataOutputDir2, <your file or dir name>)
This approach is really handy for large complicated projects, and is highly recommended.
This is also helpful, if your program is to be executed on multiple platform like Windows, Mac, Linux. You'll have to change the directory location only at one place, and everything else will work smoothly.
Additionally, following functions/handles will be useful for dealing with directory names:
Quick fix:
setwd("../") # setwd to parent folder of current working directory (getwd())
More robust:
setwd(dirname(dataInputDir)) # Setwd to parent folder of dataInputDir

How do I create a directory with a file in it, in one step?

In the terminal, is there a way to create a directory with a file in it in one step?
Currently I do this in 2 steps:
1. mkdir foo
2. touch foo/bar.txt
Apparently, touch foo/bar.txt doesn't work.
With only standard unix tools, the most direct way to create a directory and a file in this directory is
mkdir foo && touch foo/bar.txt
Unix is built around the philosophy of simple, single-purpose tools with the shell as a glue to combine them. So to create a directory and a file, you instruct a shell to run the directory creation utility then the file creation utility.
I won't swear that there isn't some bizarre way of using a standard tool that lets you do it with a single command. (In fact, there is: unpack an archive — except that you'll need to provide that archive as a file, with predefined owner, date and other metadata, or else use another command to build an archive.) But whatever it is would be convoluted.

Resources